ok I have this following code for replacing a certain parts of the a big string to something
$string = 'this <task>post</task> was a <post>post item</post>
before but now this is now a <task>task item</task>
and will become like <task>this one</task>';
$pattern = '^<task>*</task>$';
$datainmiddle = preg_replace($pattern, $replacement, $string);
$replacement = strlen(datainmid);
//I have no idea what to do next...(ノД`)・゜・。
now the thing I want to do here is the following
- I want to grab each substring in the string that starts with
<task>and ends with</task> - With each substring I grabbed, I want to grab every data in the middle of the
<task>and</task> - In every data i grabbed in the middle of the markups I want to measure their length using strlen
- Now I want to replace every substring that starts with
<task>and ends with</task>with the length of their strings in their middles.
the output must be like this
this 4 was a 9 before but now this is now a 9 and will become like 8
If you found it confusing. Please tell it to me
Okay, easy enough:
The regex uses
(.*?)to start a memory pattern which I later use to pass intostrlen().I’m using
.*?to indicate a non-greedy match (otherwise it matches everything between the first<task>and last occurrence of</task>.The
emodifier is used to evaluate the second parameter topreg_replace; this makes it possible to write'\\1'in the expression before it gets evaluated as a whole to return the string length.Hope that explanation helps a bit to understand the solution.