I have a paragraph as -
== one ===
==== two ==
= three ====
etc.
The number of =
sign vary in every row of the paragraph.
I want to write a preg_replace()
expression that will allow me to replace the texts between the =
signs.
example:
== DONE ===
==== DONE ==
= DONE ====
I tried preg_replace("/\=+(.*)\=+/","DONE", $paragraph)
but that doesn't work. Where am I going wrong?
You can use:
$str = preg_replace('/^=+\h*\K.+?(?=\h*=)/m', 'DONE', $str);
RegEx Breakup:
^ # Line start
=+ # Match 1 or more =
\h* # Match or more horizontal spaces
\K # resets the starting point of the reported match
.+? # match 1 or more of any character (non-greedy)
(?=\h*=) # Lookahead to make sure 0 or more space followed by 1 = is there
You have to place the =
s back.
Also, instead of .*
use [^=]*
(matches characters, which are not =
) so that the =
s don't get eaten up for the replacement.
Additionally, you don't have to escape =
:
preg_replace("/(=+)([^=]*)(=+)/","$1 DONE $3", $paragraph);