Convert strings such as *italic* to <i>italic</i>, but not if the newline character is within the asterisk limit
P粉262073176
P粉262073176 2024-03-29 21:43:59
0
1
372

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粉262073176
P粉262073176

reply all(1)
P粉396248578

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
  • |代码>: or
  • \*([^*]*)\*: Matches the string enclosed in *
  • s
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!