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

  1. i want grab each substring in string starts <task> , ends </task>
  2. with each substring grabbed, want grab every data in middle of <task> , </task>
  3. in every data grabbed in middle of markups want measure length using strlen
  4. 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

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -