Home>

I'm making something that can easily write HTML, but I'm having trouble with regular expressions.
If it is. # bold (character), it will be "character", if it is #strike (character), it will be "character".
I was imagining "letter".

Error message

. # bold (#strike (aa)) will result in #bold (aa).

Applicable source code
$html = $_POST ['data'];
$html = preg_replace ("/ \ # bold \ ((((?>[^ ()] +) | (? R)) *) \)/x", '<font>${1}</font>', $html);
$html = preg_replace ("/ \ # strike \ ((((?>[^ ()] +) | (? R)) *) \)/x", '<font>${1}</font>', $html);
echo $html;
<form name = "frm" method = "post" action = "/ converter">
  <input type = "submit" value = "preview" /><br>
  <textarea rows = "15" name = "data"></textarea>
</form>
Supplemental information

PHP 7.1.15
Apache/2.4.6 (CentOS)

  • Answer # 1

    (? R)is not good because it is a recursion of the entire pattern including# ~before the parenthesis.

    In brackets,
    ・ Strings other than parentheses
    # Alphanumeric characters (recursive pattern in parentheses)
    Either.

    Since the parentheses are "subpattern 1", use the recursive(? 1)of subpattern 1.

    $html = preg_replace ("/ # bold \ (((?>[^ ()] +) | # \ w + \ ((? 1) \)) \ )/",
        '<font>${1}</font>', $html);
    $html = preg_replace ("/ # strike \ (((?>[^ ()] +) | # \ w + \ ((? 1) \)) \) /",
        '<font>${1}</font>', $html);


    Incidentally, thexoption has been removed.

  • Answer # 2

      

    I'm making something that can easily write HTML

    You can make it yourself, but you may want to use the existing Markdown, etc. for ease of use.

    cebe/markdown