Good day. How to make the correct regular expression?

There is a text:

[li]text1[li]text2[/li][/li] 

With the help of this regular season:

 \[li\](.*?)\[\/li\] 

It breaks down like this:

 <li>text1[li]text2</li>[/li] 

And you need:

 <li>text1<li>text2</li></li> 
  • Tried it like that? [li] (. *) [\ / li] - Opalosolo
  • Tried, does not help. - Essle Jaxcate
  • and if so [li] ([^ [] *) [\ / li] and apply two times. - KoVadim
  • Not the fact that inside li there will be just one li. They can be as many as you like :) - Essle Jaxcate
  • so what? most importantly, that they were properly invested. Although I think this is not a regular page , but only two replacements :) - KoVadim

1 answer 1

you can do this:

 $string='[li]text1[li]text2[/li][/li]'; $pattern=':\[(/?li[^\]]*)\]:'; echo preg_replace($pattern,"<$1>",$string); 

or even like this:

 $pattern=':\[([^\]]+)\]:'; 

http://ideone.com/wj00gf

complete solution with tag verification, etc. such as:

 $pattern=':(?P<block>\[((?P<tag>[^\s\]]+)[^\]]*)\]((?&block)|[^\]]+)\[/((?P=tag))\]):'; do{ $string=preg_replace($pattern,'<$2>$4</$5>',$string,-1,$count); } while($count>0); 

http://ideone.com/Qu2fmB

should by the way notice that, according to the HTML standard, the <li> should be included only in the <ul> , <ol> or <menu> elements, i.e. code

 <li> test <li> test1 </li> </li> 

not correct


Regular expression visualization