skip to Main Content

So I’m doing my own simple Markdown formatter. I’m fixing the last of the issues when I ran into an issue with my code block formatter. For some reason it matches an extra time where there shouldn’t be anything to match.


$matches = [
    "```ncode block n```",
    "code block n"
];

private function code_block_format($matches): string
    {
        // get a line
        $regex = '/([^n]*)n?/';
        // wrap that line into <code> elem + new line
        $repl = '<code>$1</code>' . "n";
        // remove trailing linebreaks + spaces
        $matches[1] = trim($matches[1]);
        $ret = preg_replace($regex, $repl, $matches[1]); // this returns the badly formatted string
        $ret = "<pre>n" . $ret . "</pre>";
        return $ret;
    }

The preg_replace just return <code>code block</code>n but for some reason I get an extra element <code>code block</code>n<code></code>n

Any help on what in the world could be causing it to latch onto a "" string somewhere in there?

Edit

My goal is to make a codeblock element similar to what you can write here where there can be empty lines between the “` tags, so lines with simply n should be matched as well.

3

Answers


  1. Chosen as BEST ANSWER

    Okay, got an idea from the answers and found the regex I works as I want. ([^n]+n?|n) allows me to capture a line with text or the empty lines as I wanted.


  2. $regex = '/([^n]*)n?/'; returns strings that do not contain n zero or more times, so basically everything.

    Change * to +, which means it occurs one or more times.

    $regex = '/([^n]+)n?/';

    I actually can’t figure out exactly why * is returning the second group. /[^a]*/g returns two groups for any text that doesn’t include an a, and I would expect one.

    Although, your code seems needlessly complex. Are you just trying to remove white space from $match[1] with trim(), then surround it with <code></code>?

    You can just concatenate the tags onto the trimmed $matches[1]:

    return '<code>' . $matches[1] . '</code';

    Login or Signup to reply.
  3. You can try this since your initial is ZERO or more matches and can create a group where there is just a newline…

    $regex = '/([^n]+)n?/';
    

    It should output:

    <pre>
    <code>code block</code>
    </pre>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search