I have the following regular expression in my PHP code:
// markers for italic set *Text* if (substr_count($line, '*')>=2) { $line = preg_replace('#\*{1}(.*?)\*{1}#', '<i></i>', $line); }
good results.
However, when $line
holds <br>
, for example
*This is my text<br>* Some other text
Then the regex still considers the text and converts it to:
<i>This is my text<br></i> Some other text
The goal is to not translate the text if <br>
is encountered. How do I do this using regular expressions - using what is called "negative lookahead" or how do I change the existing regular expression?
Note: Strings like *This is my text*<br>Some other text<br> and again *italic*<br>END
should still be considered and converted.
Idea: Or should I break down the $line
and then use a regex to iterate over the result? !
P粉3962485782024-03-30 20:42:40
Using the match what you don't want and discard the technique, you can use this regular expression in PHP (PCRE):
\*[^*]*
\*(*SKIP)(*F)|\*([^*]*)\*
and replace it with $1
Regular expression demonstration
PHP code:
$r = preg_replace('/\*[^*]*
\*(*SKIP)(*F)|\*([^*]*)\*/'), "", $input);
illustrate:
\*
:match*
[^*]*
: Matches 0 or more non-*
characters
:match
\*
:End of match *
(*SKIP)(*F)
: PCRE verb to discard and skip this match\*([^*]*)\*
: Matches the string enclosed in
*