I'am doing a simple text editor that I need to handle creating paragraphs.
Paragraphs will be in WikiDot Syntax, long story short what i need to change:
+ paragraph 1
changes to < h1>paragraph< /h1>
++ subparagraph 1
changes to < h2>subparagraph< /h2>
How do t开发者_Python百科his in PHP?
To expand on @CrayonViolent's (in cases where the first replace interrups the second):
<?php
$content = "Hello, world
+ Big Heading
++ Smaller heading
Additional content";
function r($m){
$tag = "h".strlen($m[1]);
return "<{$tag}>{$m[2]}</{$tag}>";
}
$content = preg_replace_callback('/^(\+{1,6})\s?(.*)$/m','r', $content);
echo $content;
?>
Also added the m
(multi-line) flag to the regex for a little better matching, and will only do headers <h1>
~<h6>
.
Working example can be located here
$content = preg_replace ("~^\+\+(.*?)\n\n~",'<h2>$1</h2>',$content);
$content = preg_replace ("~^\+(.*?)\n\n~",'<h1>$1</h1>',$content);
精彩评论