Page 1 of 2

replacing a pattern with a tag...

Posted: Sat Feb 19, 2005 1:17 pm
by Todd_Z
I have files that I want to include in my pages, but instead of having <a>link</a>, i have [link]name[/link] so whats the easiest way to replace these with <a href="?page=XXX&thing=name">name</a>? I'm guessing that its preg_replace() or something along those lines... Thanks for the help

Posted: Sat Feb 19, 2005 1:25 pm
by moiseszaragoza

Code: Select all

<a href =<?php echo 'var' ; ?> target="_BLank"> get file =<?php echo 'var' ; ?> here </a>

Posted: Sat Feb 19, 2005 1:26 pm
by feyd
try looking through evilwalrus.com's set of bbcode parsers.. that should give you direction for what to do.

Posted: Sat Feb 19, 2005 1:30 pm
by shiznatix
you can use a simple function so you can do the bbcode in multiple parts of your site. somthing like this would work

Code: Select all

function BBcode($replace_this)&#123;
$patterns&#1111;1] = "|\&#1111;url\](.*?)\&#1111;/url\]|s";

$replacements&#1111;1] = "<a href="http://\$1">\$1</a>";

$replaced = preg_replace($patterns, $replacements, $replace_this);
echo $replaced;
&#125;

BBcode($name);

Posted: Sat Feb 19, 2005 1:40 pm
by Todd_Z
I don't understand something... if i have for example

[link]name[/link]

then how do i use that function to search a text and replace the above with

<a href="?page=XXX&thing=name">name</a>

Posted: Sat Feb 19, 2005 1:55 pm
by shiznatix
ok step by step..

Code: Select all

function BBcode($replace_this)&#123;
//this starts function BBcode and it is here that $name turns into 
$replace_this just for the functions use

$patterns&#1111;1] = "|\&#1111;linkl\](.*?)\&#1111;/link\]|s";
//this takes the &#1111;link]&#1111;/link] things out and keeps the leftovers(name)

$replacements&#1111;1] = "<a href="?page=XXX&thing=\$1">\$1</a>";
//this makes the html tags, the name is variable $1

$replaced = preg_replace($patterns, $replacements, $replace_this);
//this does all the fun replacements so the $patters and $replacements kinda merge together so equal $replaced

echo $replaced;
//you echo out the end result which will be <a href="?page=XXX&thing=name">name</a>

&#125;
//end the function

BBcode($name);
//you call the function $name would be the link stuff, you would make name by doing this: $name='&#1111;link]name&#1111;/link]'; or if it was done through a form post $name=$posted_field;

Posted: Sat Feb 19, 2005 2:05 pm
by feyd
be careful with that. Properly crafted usage has some potential to create links you probably don't want, depending on how your pages and/or data are parsed.

Posted: Sat Feb 19, 2005 6:12 pm
by Todd_Z
Alright, so i got that to work, now question part 2.

can i make it with two "variables" =>

[offsite=www.anothersite.com]link[/offsite]

in just one replacement? I saw in bbcode that they use two, i was wondering if thats neccessary?

Posted: Sat Feb 19, 2005 6:27 pm
by feyd
it requires two somewhat simple ones, or one very complex one. Personally, I'd do the simple route :)

use the [url] regexes from phpbb, or similar for that. It's a lot easier.

Posted: Sat Feb 19, 2005 8:47 pm
by shiznatix
add this to the function i originally wrote for you,

Code: Select all

$patterns&#1111;2] = "|\&#1111;offsite=(.*?)\](.*?)\&#1111;/offsite\]|s";

$replacements&#1111;2] = "<a href="http://\$1">\$2</a>";
put those where they belong inside the function.

Posted: Sat Feb 19, 2005 8:59 pm
by feyd
may want to make the pattern a bit smarter.. :roll:

Posted: Sat Feb 19, 2005 10:39 pm
by Todd_Z
What would be the problem with using that method?

Posted: Sat Feb 19, 2005 11:03 pm
by feyd
both of Shiz's patterns and replacements are done blind. It is very simple to inject unwanted results into the final code. For instance, I could add Javascript event hooks into the link, or worse yet, break the page layout, or given the proper circumstances, add malicious code against someone browsing the page.

Posted: Sun Feb 20, 2005 12:31 pm
by shiznatix
its true, but i am having trouble figuring out how to make these "smart", fyed why dont you post some code that might help us all out?

Posted: Sun Feb 20, 2005 6:15 pm
by McGruff
I think you really need a state engine for BBcode (nested tags..?). That means OOP, of course - and hopefully unit tests. Tests are particularly important in security-sensitive code like BBcode tag processing. You can meticulously throw all kinds of badness at your code and see if it comes up trumps.

Tests also serve as documentation (but unlike badly-maintained docs, they never lie).