Forum Moderators: coopster

Message Too Old, No Replies

PHP regex, wiki - style

isolate and utilise text between brackets

         

Cotsan

9:41 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



I need to auto generate links to other parts of my site within text, much as wikis do using square brackets. I've tried all sorts, testing each with a regex tester and have got nowhere (maybe I should try and sleep instead).

My users can currently input text to a database with a title for each piece of text.

I'd like them to be able to put <<double lt and double gt>> around words in their text that are titles for other pieces of text.

When the text is retrieved from the database, any characters within << >> will form a get statement appended to a url to make the link to a page showing the associated text.

Sorry if its not clear, I've re-drafted this post a few times. Any help appreciated, thanks.

coopster

10:14 pm on Feb 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like a good place to use a preg_replace() [php.net] function. Locate the pattern and replace it with the url and the trapped keyword. Something like:
$pattern = "/<<(.*)>>/is"; 
$text = preg_replace($pattern, "<a href=\"somescript.php?keyword=$1\">$1</a>", $text);

That's not tested, just off the top of my head. Should get you started though.