Jump to content


Regex help


4 replies to this topic

#1 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 26 June 2006 - 05:57 PM

I've been playing with the BBCode Parser from the tutorial here on this board.

It's worked great, but one thing I've always tried to figure out is how to get it to parse an http:// address to a link without the person putting the [url="http://%20tags.Well,%20I%20think%20I've%20got%20it,%20but%20it%20doesn't%20quite%20STOP%20where%20I%20want%20it%20to.
$url%20=%20"http://".$_SERVER['HTTP_HOST'];

function%20BBCode%20($string)%20{
global%20$url;
$search%20=%20array(
	'@\[(?i)b\](.*?)\[/(?i)b\]@si',
	'@\[(?i)i\](.*?)\[/(?i)i\]@si',
	'@\[(?i)u\](.*?)\[/(?i)u\]@si',
	'@\[(?i)size=(.*?)\](.*?)\[/(?i)size\]@si',
	'@\[(?i)color=(.*?)\](.*?)\[/(?i)color\]@si',
	'@\[(?i)center\](.*?)\[/(?i)center\]@si',
	'@\[(?i)left\](.*?)\[/(?i)left\]@si',
	'@\[(?i)right\](.*?)\[/(?i)right\]@si',
	'@\[img\](.*?)\[/img\]@si',
	'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
	'@\[(?i)url\](.*?)\[/(?i)url\]@si',
	[u][b]'@^(?:http://)?([^/]+)@i',[/b][/u]
	'@\[(?i)bbcode\]@si'
);
$replace%20=%20array(
	'<b>\\1</b>',
	'<i>\\1</i>',
	'<u>\\1</u>',
	'<font%20style="font-size:%20\\1em;">\\2</font>',
	'<font%20style="color:%20\\1;">\\2</font>',
	'<center>\\1</center>',
	'<p%20style="text-align:%20left;">\\1</p>',
	'<p%20style="text-align:%20right;">\\1</p>',
	'<img%20src="\\1"%20border="0"%20alt="User%20Image"%20/>',
	'<a%20href="'.$url.'/redirect.php?url=\\1"%20target="_blank">\\2</a>',
	'<a%20href="'.$url.'/redirect.php?url=\\1"%20target="_blank">\\1</a>',
	[u][b]'<a%20href="'.$url.'/redirect.php?url=\\1"%20target="_blank">http://\\1</a>',[/b][/u]
);
return%20preg_replace($search%20,%20$replace,%20$string);
}

if%20(isset($_POST['submit']))%20{
	$data_translated%20=%20BBCode(nl2br(htmlspecialchars(stripslashes(strip_tags($_POST['data'])))));
	$pre_data%20=%20$_POST['data'];
	unset($_POST['data']);
}

I%20bold%20and%20underlined%20the%202%20main%20parts.%20it%20seems%20to%20be%20adding%20a%20space%20and%20part%20of%20a%20<br%20/>%20to%20the%20link


P.S.%20%5burl="http://www.spatialvisionary.com"%5dhttp://www.spatialvisionary.com" target="_blank"> tags.

Well, I think I've got it, but it doesn't quite STOP where I want it to.

$url = "http://".$_SERVER['HTTP_HOST'];

function BBCode ($string) {
global $url;
$search = array(
	'@\[(?i)b\](.*?)\[/(?i)b\]@si',
	'@\[(?i)i\](.*?)\[/(?i)i\]@si',
	'@\[(?i)u\](.*?)\[/(?i)u\]@si',
	'@\[(?i)size=(.*?)\](.*?)\[/(?i)size\]@si',
	'@\[(?i)color=(.*?)\](.*?)\[/(?i)color\]@si',
	'@\[(?i)center\](.*?)\[/(?i)center\]@si',
	'@\[(?i)left\](.*?)\[/(?i)left\]@si',
	'@\[(?i)right\](.*?)\[/(?i)right\]@si',
	'@\[img\](.*?)\[/img\]@si',
	'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
	'@\[(?i)url\](.*?)\[/(?i)url\]@si',
	[u][b]'@^(?:http://)?([^/]+)@i',[/b][/u]
	'@\[(?i)bbcode\]@si'
);
$replace = array(
	'<b>\\1</b>',
	'<i>\\1</i>',
	'<u>\\1</u>',
	'<font style="font-size: \\1em;">\\2</font>',
	'<font style="color: \\1;">\\2</font>',
	'<center>\\1</center>',
	'<p style="text-align: left;">\\1</p>',
	'<p style="text-align: right;">\\1</p>',
	'<img src="\\1" border="0" alt="User Image" />',
	'<a href="'.$url.'/redirect.php?url=\\1" target="_blank">\\2</a>',
	'<a href="'.$url.'/redirect.php?url=\\1" target="_blank">\\1</a>',
	[u][b]'<a href="'.$url.'/redirect.php?url=\\1" target="_blank">http://\\1</a>',[/b][/u]
);
return preg_replace($search , $replace, $string);
}

if (isset($_POST['submit'])) {
	$data_translated = BBCode(nl2br(htmlspecialchars(stripslashes(strip_tags($_POST['data'])))));
	$pre_data = $_POST['data'];
	unset($_POST['data']);
}

I bold and underlined the 2 main parts. it seems to be adding a space and part of a <br /> to the link


P.S. [url="http://www.spatialvisionary.com"]http://www.spatialvisionary.com[/url] there's the working page.

Edited by SpatialVisionary, 26 June 2006 - 05:57 PM.


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 27 June 2006 - 07:47 AM

just a tip, with this line of code:

BBCode(nl2br(htmlspecialchars(stripslashes(strip_tags($_POST['data'])))));

you dont need htmlspecialchars() AND strip_tags(). htmlspecialchars will turn > into &lt;, so there will be no tags that need to be stripped, either one of them will do really. But use addslashes() which cancles out any quotes when inputting data by a form.

Matt

#3 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 27 June 2006 - 10:17 AM

View Postdeadly, on Jun 27 2006, 12:46 PM, said:

just a tip, with this line of code:

BBCode(nl2br(htmlspecialchars(stripslashes(strip_tags($_POST['data'])))));

you dont need htmlspecialchars() AND strip_tags(). htmlspecialchars will turn > into &lt;, so there will be no tags that need to be stripped, either one of them will do really. But use addslashes() which cancles out any quotes when inputting data by a form.

Matt

thanks. :)

I suppose I should remove the stripslashes too? :love:

#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 27 June 2006 - 10:45 AM

if that bit of code is used for entering data to the db then you really only need:

BBCode(htmlspecialchars(addslashes($_POST['data'])));

stripslahes() is used to take away any '/' put infront of quotes when pulling stuff out of the db, i changed it to addslahes() which i explained b4.

i tend to use the nl2br() when i pull data from the database, i guess you can keep that, but i tend to put it on when stuff comes out

and just removed the strip_tags() as not really needed

#5 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 27 June 2006 - 10:52 PM

View Postdeadly, on Jun 27 2006, 03:45 PM, said:

if that bit of code is used for entering data to the db then you really only need:

BBCode(htmlspecialchars(addslashes($_POST['data'])));

stripslahes() is used to take away any '/' put infront of quotes when pulling stuff out of the db, i changed it to addslahes() which i explained b4.

i tend to use the nl2br() when i pull data from the database, i guess you can keep that, but i tend to put it on when stuff comes out

and just removed the strip_tags() as not really needed

cool, thanks. <3

I made a few of those adjustments and think I've come up with a fix, at least better than it was.

'@^(?:http://)?([^ ]+)@i'

user will still have to put a space after the address for the bbcode function to translate right, but it's better than it was.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users