Jump to content

BBTags Phraser


Sars

Recommended Posts

Hi everyone - first post. Anyywho...

 

I'm currently working on a BBphraser for an upcoming website (similar to Newgrounds.com - ZombieCreations.org/test if you want a sneak peek...).

 

It's all working except for some of my preg match code; this is understandable because my understandind of preg match patterns is limited.

 

Issue Outline:

When phrasing something like

I like [url=http://www.google.com]Google[/url].
I dislike [url=http://www.yahoo.com]Yahoo[/url].

 

Both links come out with their href's equaling http://www.google.com - this goes for colour tags, etc. They all are set to the first preg match's...uhh..match.

 

The Code:

if(preg_match("^\[url\](.+?)\[/url\]^im",quote_smart($string),$urls)) {
  $string = preg_replace_callback('^\[url\](.+?)\[/url\]^im',create_function('$urls','return \'<a href="'.trim($urls[1]).'" target="_blank">'.trim($urls[1]).'</a>\';'),$string);
}

 

Also - I'm looking for a way to close tags at the end of all code if they're not closed - to keep people from breaking the site. Ideas?

 

And last but not least - PLEASE don't send me to a 'premade BBtags phraser' because I've got alot of custom things going on in mine; and I like to have an indepth understanding of my code where possible (sounds ironic due to my comments eairler in this post, eh?)

 

Thanks,

Sars

Link to comment
Share on other sites

It looks to me like you're overcomplicating the issue. You don't need to search and then replace - you can just use preg_replace.

 

preg_match() only returns the first match - you have to use preg_match_all() to find all matches in a string. I suspect this is causing all of your URLs to be replaced by the first one.

 

Something like this should work:

 

<?php
$patterns[] = "|\[url\](.*?)\[/url\]|is";
$patterns[] = "|\[url=(.*?)\](.*?)\[/url\]|is";

$replacements[] = '<a href="$1">$1</a>';
$replacements[] = '<a href="$1">$2</a>';

preg_replace($patterns,$replacements,$string);
?>

Link to comment
Share on other sites

Thanks for the help - that seems to be working.

 

Any idea how to do something like...

 

if(preg_match("^\[size=(.+?)\](.+?)\[/size\]^im",quote_smart($string),$sizes)) {
    	 if ($sizes[1]=='tiny') {
    	   $sizes[1]='7pt';
    	 }
    	 if ($sizes[1]=='small') {
    	   $sizes[1]='8pt';
    	 }
    	 if ($sizes[1]=='normal') {
    	   $sizes[1]='10pt';
    	 }
    	 if ($sizes[1]=='large') {
    	   $sizes[1]='14pt';
    	 }
    	 if ($sizes[1]=='huge') {
    	   $sizes[1]='20pt';
    	 }
    	 else {
    	   //Error
    	 }
      $string = preg_replace_callback('^\[size=(.+?)\](.+?)\[/size\]^im',create_function('$sizes','return \'<span style="font-size:'.trim($sizes[1]).'">'.trim($sizes[2]).'</span>\';'),$string);
    }

 

Because I'm not sure how to check what's matched to see if its valid with your method, etc.

 

Either that or show me how to use preg match all and print out the results, at the moment I just get the words replaced with 'Array' and I'm unsure of what I should be passing to get the correct result from the array.

 

Thanks for the help so far! :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.