regex - PHP: preg_split -


i need on regular expression here.

i want php able split string in sections of arrays such substring enclosed <%me %> in own slot.

so example,

hi there how <%me date(); %> => {"hi there how ", "<%me date(); %>}  hi there how you<%me date(); %> => {"hi there how you", "<%me date(); %>} hi there how you<%me date(); %>goood => {"hi there how you", "<%me date(); %>, "good" hi there how you<%me date(); %> => {"hi there how you", "<%me date(); %>}, " good"} 

note white space won't stop tags getting parsed in.

on capturing splitting delimiter in preg

you can use preg_split_delim_capture split on , capture delimiter.

remember put delimiter in capturing group (…) work properly.

here's example:

$text = 'abc123xyz456pqr';  $parts = preg_split('/(\d+)/', $text, -1, preg_split_delim_capture);  print_r($parts); 

this prints (as seen on ideone.com):

array (     [0] => abc     [1] => 123     [2] => xyz     [3] => 456     [4] => pqr ) 

references


back question

in case, can try delimiter pattern (<%me[^%]+%>). is:

  • <%me, literally
  • [^%]+, i.e. %
  • %>, literally
  • the whole thing captured in group 1

if % can appear in tag, can try (<%me.*?%>).

here's example:

$text = 'prefix<%me date() %>suffix';  $parts = preg_split('/(<%me[^%]+%>)/', $text, -1, preg_split_delim_capture);  print_r($parts); 

the above prints (as seen on ideone.com):

array (     [0] => prefix     [1] => <%me date() %>     [2] => suffix ) 

related questions


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -