replacing a pattern with a tag...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

replacing a pattern with a tag...

Post 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
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post by moiseszaragoza »

Code: Select all

<a href =<?php echo 'var' ; ?> target="_BLank"> get file =<?php echo 'var' ; ?> here </a>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try looking through evilwalrus.com's set of bbcode parsers.. that should give you direction for what to do.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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);
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

may want to make the pattern a bit smarter.. :roll:
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

What would be the problem with using that method?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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).
Post Reply