Starts and Ends with PHP Regex -
ok have following code replacing parts of big string
$string = 'this <task>post</task> <post>post item</post> before <task>task item</task> , become <task>this one</task>'; $pattern = '^<task>*</task>$'; $datainmiddle = preg_replace($pattern, $replacement, $string); $replacement = strlen(datainmid); //i have no idea next...(ノД`)・゜・。 now thing want here following
- i want grab each substring in string starts
<task>, ends</task> - with each substring grabbed, want grab every data in middle of
<task>,</task> - in every data grabbed in middle of markups want measure length using strlen
- now want replace every substring starts
<task>, ends</task>length of strings in middles.
the output must this
this 4 9 before 9 , become 8
if found confusing. please tell me
okay, easy enough:
preg_replace('#<task>(.*?)</task>#e', "strlen('\\1')", $str); the regex uses (.*?) start memory pattern later use pass strlen().
i'm using .*? indicate non-greedy match (otherwise matches between first <task> , last occurrence of </task>.
the e modifier used evaluate second parameter preg_replace; makes possible write '\\1' in expression before gets evaluated whole return string length.
hope explanation helps bit understand solution.
Comments
Post a Comment