replacing a pattern with a tag...
Moderator: General Moderators
replacing a pattern with a tag...
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
- moiseszaragoza
- Forum Commoner
- Posts: 87
- Joined: Sun Oct 03, 2004 4:04 pm
- Location: Ft lauderdale
- Contact:
Code: Select all
<a href =<?php echo 'var' ; ?> target="_BLank"> get file =<?php echo 'var' ; ?> here </a>- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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){
$patternsї1] = "|\їurl\](.*?)\ї/url\]|s";
$replacementsї1] = "<a href="http://\$1">\$1</a>";
$replaced = preg_replace($patterns, $replacements, $replace_this);
echo $replaced;
}
BBcode($name);- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
ok step by step..
Code: Select all
function BBcode($replace_this){
//this starts function BBcode and it is here that $name turns into
$replace_this just for the functions use
$patternsї1] = "|\їlinkl\](.*?)\ї/link\]|s";
//this takes the їlink]ї/link] things out and keeps the leftovers(name)
$replacementsї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>
}
//end the function
BBcode($name);
//you call the function $name would be the link stuff, you would make name by doing this: $name='їlink]nameї/link]'; or if it was done through a form post $name=$posted_field;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?
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?
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
add this to the function i originally wrote for you,
put those where they belong inside the function.
Code: Select all
$patternsї2] = "|\їoffsite=(.*?)\](.*?)\ї/offsite\]|s";
$replacementsї2] = "<a href="http://\$1">\$2</a>";- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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).
Tests also serve as documentation (but unlike badly-maintained docs, they never lie).