Jek Jek - 7 months ago 28
PHP Question

Split string and add part on it

So say that I have a string like

2x + 2y
, and I want to split these strings.

I want the split to be done and it will look like
2 * x + 2 * y
after it has been parsed or changes have been made to it.

I looked on the PHP Manual for
but I couldn't seem to find the right thing for me, I was thinking it might look for a
number + letter
then replace it using the
in the middle of it, but I don't know how to achieve this.


Try This Code

$string = "2x + 2y";
$result = preg_replace("/([0-9]+)([A-Za-z])/", "$1 * $2", $string);
echo $result;