PHP regular Unknown Modifier error resolution

  • 2020-03-31 20:24:38
  • OfStack

The following regular:

$a = '2 < Span> < Nobr> Tong< Span class = "h" > Dong< / span> < / nobr> < Br> Dong < / span> 3 ';
Echo preg_replace (' / < Span class = "h" > [^ <] *? < / span> / ', ' ', $a);

You will be prompted:

Warning: preg_replace(): Unknown modifier 'p' in E:\phpLearn\test.php on line 12

The reason is:

In regular mode, the delimiter is /, but the regular also contains /, so this error occurs when PHP mistakenly USES the following < / span> The slash in is the closing delimiter.

Solutions:

1. Add an escape:

Echo preg_replace (' / < Span class = "h" > [^ <] *? < \ / span> / ', ' ', $a);

2. Change other delimiters: e.g

Echo preg_replace (' {< Span class = "h" > [^ <] *? < / span> } ', ' ', $a);

Related articles: