Issue
$str = "Hello 1234567 Stack 56789 Overflow 12345";
$str = preg_replace('/([0-9] )/', ',', $str);
I want this "Hello 1234567, Stack 56789, Overflow 12345,..."
Solution
Use
preg_replace('/\d(?=\s)/', '$0,', $str)
See proof.
Expression explanation
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
Answered By - Ryszard Czech Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.