Thursday, 15 April 2010

regex - Trying to imitate Markdown's blockquote functionality in a PHP WYSIWYG -



regex - Trying to imitate Markdown's blockquote functionality in a PHP WYSIWYG -

i'm building wysiwyg ground academic exercise , i've run snag. i'm trying build wysiwyg work using basic markdown methods, using asterisks bold/italic, haskmarks headings, etc. however, i've run issue blockquotes. here's code i'm using process input blockquotes:

$content = '$_post['content']; while (preg_match('^>\s(.*)$', $content)) { $content = preg_replace('^>\s(.*)$', '<blockquote>$1</blockquote>', $content); };

basically looks line starts 'greater than' sign, extracts text , places in blockquote tags so:

input: > blockquote. output: <blockquote>this blockquote.</blockquote>

that's great, markdown can take multiline blockquote , turn single blockquote. example:

input: > blockquote > decided separate across > several lines. output: <blockquote>this blockquote decided separate across several lines.</blockquote>

i'd mimic functionality current code i'll end this:

output: <blockquote>this blockquote that</blockquote><blockquote>i decided separate across</blockquote><blockquote>several lines.</blockquote>

i'm not sure how concatenate blockquotes. 1 approach thought of changing each line, doing new search </blockquote><blockquote> without double line break between them seems inefficient. code become:

$content = '$_post['content']; while (preg_match('^>\s(.*)$', $content)) { $matched = true; $content = preg_replace('^>\s(.*)$', '<blockquote>$1</blockquote>', $content); }; if ($matched) { $content = preg_replace('</blockquote>(\n|\r)?<blockquote>', '', $content); };

i suppose work, sense there's improve method utilizes regex lookahead , grab of lines. unfortunately, i'm clueless be.

replace <p> tag want :)

<?php $text = ' line 1 line 2 > blockquote blockquote blockquote blockquote blockquote > blockquote blockquote blockquote blockquote blockquote > blockquote blockquote blockquote. line 3 > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa '; function blockquota($matches){ $mytext = str_replace(array("> ", ">"), '' , $matches[0]); $mytext = '<p>'.$mytext.'</p>'; homecoming $mytext; } $pattern = '/(>)([^\v]+)(\v*((>)([^\v]+)))*/'; echo htmlspecialchars(preg_replace_callback($pattern, 'blockquota', $text)); ?>

output:

line 1 line 2 <p>blockquote blockquote blockquote blockquote blockquote blockquote blockquote blockquote blockquote blockquote blockquote blockquote blockquote.</p> line 3 <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>

php regex markdown

No comments:

Post a Comment