I have this string: $str = "(he+is+genius*2)/clever";
which looks like this in array;
Array (
[0] => (
[1] => he
[2] => +
[3] => is
[4] => +
[5] => genius
[6] => )
[7] => *
[8] => and
[9] => /
[10] => clever )
What I want to do is placing dollar sign $ before each string present in $str
but ignoring non-alphanumeric and numbers.
At the end i want to have something which looks like this;
$newstr = "($he+$is+$genius*2)/$clever";
For each value, check if the first char (or the whole value) is made of characters with
ctype_alpha
, then prepend with$
:Output :
Second solution, checking if it has a char at any position :
Just map the array using
array_map
and check if their values are string or not withctype_alpha
, concatenating the$
to it.