php - preg_replace match and replace exact count of characters -


how can add count or whatever else preg_replace replaces when encounters 2 or 3 > , not more or less that?

original

$str = '>>this code>> text >>>this link>>>. >>>>this need not replaced>>>>';  $patterns = array('#>>((?!>[^>]).+?)>>#','#>>>((?!>[^>]).+?)>>>#'); $actions = array('[code]$1[/code]','[a]$1[/a]');  echo preg_replace($patterns, $actions, $str );  //output [code]this code[/code] text >[code]this link[/code]>. [code]>>this need not replaced[/code]>> 

expected

[code]this code[/code] text [a]this link[/a]. >>>>this need not replaced>>>> 

what i'm trying here is, match >> , >>> , not else, third 1 in string. current end doing matching in there.

i tried adding {2} , {3} $patterns :

$patterns = array('#>>{2}((?!>[^>]).+?)>>{2}#','#>>>{3}((?!>[^>]).+?)>>>{3}#');,

but wrecks things further.

i plan on adding more patterns there later, how can tell regex 2 or 3 > (or more later) , not > in string?

$str = '>>this i>s code>> text >>>this >> li>nk>>>. >>>>this need not replaced>>>> >>bad > stuff>>> ';  $patterns = array(     '#(?<!>)(>{2})(?!>)(.*?)(?<!>)\1(?!>)#',     '#(?<!>)(>{3})(?!>)(.*?)(?<!>)\1(?!>)#',     ); $actions = array('[code]$2[/code]','[a]$2[/a]');  echo preg_replace($patterns, $actions, $str ); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -