preg_replace with Regex to add a tag

2

Does anyone know how to do regex select the phrase and add a tag?

<li>
  <p>b) texto muito longo;</p>
</li>


$FechasemClassAlinea = '<p>[a-z])_.*</p>\n</li>$';

$FechacomClassAlinea = '<p>[a-z])_.*</p>\n</li>\n</ol>$';

echo preg_replace($FechasemClassAlinea, $FechacomClassAlinea, $documento);

I would like to return this value:

  <li>
    <p>b) texto muito longo;</p>
  </li>
</ol>
    
asked by anonymous 03.05.2015 / 18:58

2 answers

2
preg_replace('/(<p>[a-z]\).*<\/p>\s*<\/li>)/', '$1'."\n</ol>", $documento);

Explaining:

In the preg_replace function, the first parameter is the regex that should start and end with / within the regex, it is necessary to escape the characters that have a special meaning in the regex as) and / in this case, by adding a backslash before character, \ s represents space / tab characters / newline etc, should be used instead of the \ n you put, lastly put everything inside parentheses () to create a group, since that part should continue after replacement .

In the second parameter we put what will replace the selected text in the regex, in this case we must keep the group selected in the regex, to pick a group should use $ followed by the number of the group in ascending order starting with 1, and in the end we concatenamos the tag to be added.

    
03.05.2015 / 19:35
0

I was able to accomplish what I intended with jquery

this is the code

$('p').filter(function(){return $(this).text().match(/^[a-z]\)\s/) }).parent().addClass( "alinea");
    
05.05.2015 / 21:04