Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this Regular Expression do

$pee = preg_replace( '|<p>|', "$1<p>", $pee );

This regular expression is from the Wordpress source code (formatting.php, wpautop function); I'm not sure what it does, can anyone help?

Actually I'm trying to port this function to Python...if anyone knows of an existing port already, that would be much better as I'm really bad with regex.

like image 498
Roy Tang Avatar asked Sep 18 '26 02:09

Roy Tang


2 Answers

The preg_replace() function - somewhat confusingly - allows you to use other delimiters besides the standard "/" for regular expressions, so

"|<p>|"

Would be a regular expression just matching

"<p>" 

in the text. However, I'm not clear on what the replacement parameter of

"$1<p>" 

would be doing, since there's no grouping to map to $1. It would seem like as given, this is just replacing a paragraph tag with an empty string followed by a paragraph tag, and in effect doing nothing.

Anyone with more in-depth knowledge of PHP quirks have a better analysis?

like image 169
Jay Avatar answered Sep 19 '26 15:09

Jay


wordpress really calls a variable "pee" ?

I'm not sure what the $1 stands for (there are no braces in the first parameter?), so I don't think it actually does anything, but i could be wrong.

like image 33
I.devries Avatar answered Sep 19 '26 16:09

I.devries



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!