I have a line, something like this:
$str ="it is a test string.";
Now I need to check all characters that are multiples of 4 (plus the first character). eg:
1 => i 4 => i 8 => [space] 12 => t 16 => r 20 => .
Now I need to compare them with Y ( Y is a variable (character), for example Y = 'r' here). So I want to replace Y with X ( X also a variable (character), for example X = 'm' ).
So I need this output:
it is a test stming.
Here is my solution : I can do this with some PHP function:
strlen($str) : count the number of characters (named $sum )$sum / 4 : To find characters that are multiples of 4substr($str, 4,1) : to select a specific character (named $char ) {problem )if ($char == 'r') {} : comparestr_replace('r','m',$char) : replace
And then combining all $char with each other.
But my solution has two problems :
substr() not considered a [space] character (as I mentioned above) Combination of characters- a bit complicated. (This is necessary for waste treatment).
Ok, is there any solution? I like to do this with REGEX, is this possible?
string php regex
Shafizadeh
source share