Jump to content

preg_replace() and regular expressions


pswoo

Recommended Posts

I've written some code for marking up plain text, looks like this:

function MarkUp($plaintext) {
	$search[] = "/\</";
		$replace[] = "&#60;";
	$search[] = "/\>/";
		$replace[] = "&#62;";
	$search[] = "/(\n|)\*\)(.*?);(\n|)/is";
		$replace[] = "<li>$2</li>";
	$search [] = "/\<li\>(.*?)\<\/li\>\n/";
		$replace[] = "<li>$1</li>";
	$search[] = "/\\_(.*?)\_/is";
		$replace[] = "<u>$1</u>";
	$search[] = "/\*\*(.*?)\*\*/is";
		$replace[] = "<strong>$1</strong>";
	$search[] = "/\*(.*?)\*/is";
		$replace[] = "<em>$1</em>";
	$search[] = "/\((.*?)\)\[(.*?)\]/";
		$replace[] = "<a href=\"index.php?filter=$2&type=title\">$1</a>";
	$search[] = "/\n/is";
		$replace[] = "<br>";
	$search[] = "/  /";
		$replace[] = "  ";
	$string = preg_replace($search, $replace, $plaintext);
	return $string;
}

 

But I'm having some trouble with marking up lists this way.

 

*)item one

*)item two

*)item three

 

.. should render as an unordered list, as should *)item one *)item two *)item three (no line breaks). But replacing the pattern "/(\n|)\*\)(.*?)(\n|)/is" with "<li>$2</li>" isn't working. I changed the format to *)item one; *)item two; (adding a semi-colon delimiter), and that worked - but I'd like to do without that unnatural separator.

 

I'm fairly inexperienced with regular expressions, can anyone throw me a tip or give it a shot?

Link to comment
https://forums.phpfreaks.com/topic/171053-preg_replace-and-regular-expressions/
Share on other sites

Well I'd like the list items to end on a line break. For the inline format *)one; *)two; *)three; the semi-colons are alright, but otherwise plaintext like

*)one

*)two

*)three

should be rendered as three list items without the need for the semi-colon at the end of each line. Although I guess that would make problems if I ever wanted nested lists... /shrug. As I said I'm pretty unfamiliar with regular expressions, and I'm not even sure if this is the best method (using preg_replace) for marking up plain text.

I got it working, even though it's a bit messy:

 

<?php
//for lists on several lines
$search[] = '~(?:^\s*\*\).+$\s+)+~me';
$replace[] = "'<ul><li>' . implode('</li><li>', array_slice(explode('*)', '$0'), 1)) . '</li></ul>'";
//for lists on a single line
$search[] = '~(?:\*\)[^;]*?;\s*)+~e';
$replace[] = "'<ul><li>' . implode('</li><li>', array_slice(explode('*)', '$0'), 1)) . '</li></ul>'";
?>

 

One thing missing is the removal of the semi colons from the inline lists. Will take some more code to add. And a tip for formatting a nice search/replace array:

 

<?php
$replace = array(
'~1st pattern~' => 'replacement',
'~2nd pattern~' => 'replacement',
//...
);
$str = preg_replace(array_keys($replace), $replace, $str);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.