Thursday, 3 October 2013

regex for html anchors in titles

regex for html anchors in titles

After a lot of messing around and the help of you guys I got a nice system
that makes a little list of all the titles in my html text. This is done
with a regex which works fine until I try to add the URL's
Original:
$txt = preg_replace("#\[h2\](.+?)\[/h2\](.+?(?=\[h1\]|\[h2\])|.+$)#is",
"<h2 style='display:inline;'>\\1</h2>", $txt);
$txt = preg_replace("#\[h1\](.+?)\[/h1\](.+?(?=\[h1\]|<h2)|.+$)#is", "<h1
style='display:inline;'>\\1</h1>", $txt);
What I'm trying to make it
$txt = preg_replace("#\[h2\](.+?)\[/h2\](.+?(?=\[h1\]|\[h2\])|.+$)#is",
"<li><a href=\"#\\1>\\1</a></li>", $txt);
$txt = preg_replace("#\[h1\](.+?)\[/h1\](.+?(?=\[h1\]|<li)|.+$)#is",
"</ul><a href=\"#\\1>\\1</a><ul>", $txt);

No comments:

Post a Comment