Good day!

There are two lines:

Привет! Привет! 

It is necessary to wrap the contents of those lines that do NOT begin with two or more spaces in the span (that is, no spaces at the beginning).

I do this:

 // если строка не начинается с двух и более пробелов, то обернуть её содержимое preg_replace( "/(?<!^[ ]{2,})([\s\S]*?)(\<br\>)/miu", "<span style=\"color: red;\">$1</span>$2", $match ); 

Does not work. I would be grateful for the help.

  • And what to do with the spaces, leave them? - Majestio
  • Just need to fully process only those lines that do not have more than two spaces in the beginning. The rest (with spaces in the beginning), just skip - Pavel V

1 answer 1

 <?php $str="Привет<br>\n Привет2<br>\n Привет3!<br>"; preg_match_all("/^(?![ ]{2,})([\s\S]*?)(\<br\>)$/miu", $str, $mtc, PREG_SET_ORDER); print_r($mtc); ?> 

Further work with the $mtc .

Or, if you want to leave the lines unprocessed, and not delete them completely, then:

 <?php $str="Привет<br>\n Привет2<br>\n Привет3!<br>"; echo preg_replace("/^(?![ ]{2,})([\s\S]*?)(\<br\>)$/miu", '<span style=\"color: red;\">$1</span>$2', $str); ?>