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
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.$regex = '/([^n]*)n?/';
returns strings that do not containn
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 ana
, 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';
You can try this since your initial is ZERO or more matches and can create a group where there is just a newline…
It should output: