0

I have a string that's throwing up a couple php errors.

I'm using some regex to strip <p> tags from images and iframes.

I'm also using regex to replace a <br\> with two <br\><br\>.

I'm using it on $content in wordpress.

It's throwing an error and therefore nothing is being returned in the_content();

If I remove the functions the content appears fine, though without the formatting changes.

I assume if I fix these errors the content will work fine but I don't know how to adjust the regex.

Anyway here are the errors:

[12-Aug-2014 19:46:46 UTC] PHP Warning:  preg_replace(): Unknown modifier '&gt;' in /functions.php on line 101
[12-Aug-2014 19:46:46 UTC] PHP Warning:  preg_replace(): Unknown modifier 'f' in /functions.php on line 102
[12-Aug-2014 19:46:46 UTC] PHP Warning:  preg_replace(): Unknown modifier '?' in /functions.php on line 133

And of course the code:

function filter_ptags($content){
    $content = preg_replace('/<p>s*(<a .*>)?s*(<img .* />)s*(</a>)?s*</p>/iU', '123', $content);
    return preg_replace('/<p>s*(<iframe .*>*.</iframe>)s*</p>/iU', '1', $content);
}
function strip_content($content){
    $content = preg_replace(array('/(<brs*/?>s*){1,}/'), array('<br/><br/>'), $content);
}

I really need to spend some time getting a grasp on regex...

UzumakiDev
  • 1,286
  • 2
  • 17
  • 39

1 Answers1

1
  • You need to escape / in a regex expression. It should be \/

  • It should be \s* instead of s* to match zero or more white spaces.

Change:

(<brs*/?>s*){1,}

To:

(<br\s*\/?>\s*){1,}

Here is online demo where you can validate your regex expression as well.

Braj
  • 46,415
  • 5
  • 60
  • 76