[PHP]Regex Expression - Does the URL contain "http"?

Joined
Apr 7, 2009
Messages
647
Reaction score
26
Hey,
This is my current BBCode parser:

PHP:
function parseBBCode($message)
{
	$message = wordwrap($message, 160, "<br />", true);

	#Lets use a regex instead of str_replace.#
	$message = preg_replace('|\[b\](.+?)\[\/b\]|i', '<b>$1</b>', $message);
	$message = preg_replace('|\[i\](.+?)\[\/i\]|i', '<i>$1</i>', $message);
	$message = preg_replace('|\[u\](.+?)\[\/u\]|i', '<u>$1</u>', $message);
$message = preg_replace('|\[img\](.+?)\[\/img\]|i', '<img src="$1" border="0" alt="$1" title="$1">', $message);
	$message = preg_replace('|\[url="(.+?)"\](.+?)\[\/url\]|i', '<a href="$1" target="_blank">$2</a>', $message);
}

Problem is, if the url for the last regex does not have http, it goes to

127.0.0.1/$1.

How can I check if the url contains 'http', and if not, add 'http'?

I know it has something to do with parse_url.
 
Last edited:
PHP:
$prefix = "http://";
$text = "http://forum.ragezone.com";
$location_of_prefix = strpos($text,$prefix);
if($location_of_prefix != 1) {
   $text = $prefix + $text + "";
}
Since I'm not sure about the index of the first char in a string, i suggest trying both $location_of_prefix != 1 and $location_of_prefix != 0 just to be sure.
 
There are 2 types of bbcodes, the first does not have a parameter inside the actual tag, the second does.

Instead of making a bunch of different $message assignments, you can do them by using two arrays (one for each type of bbcode) and each bbcode is a string inside of them. You will then be able to use a loop (preferably foreach) in order to go through them all, and shorten your code by a lot.
 
On second thought, is there a way just to not make the <a> tag go to <site>/<link> ?

Like on an ordinary website, < a href="bob.com">bob< /a>
would lead too

<site>/bob.com.

But on vB and other forum software, it would lead to http://www.bob.com

Bob

So basically, how can I make it an absolute URI, instead of a relative URI?
 
Last edited:
PHP:
$text = preg_replace('|\[url\](.+?)\[\/url\]|i',		 "<a target=\"_blank\" href=\"\\1\">\\1</a>", $text);
$text = preg_replace("/\[url=(.+?)\\](.+?)\[\/url\]/is", "<a target=\"_blank\" href=\"\\1\">\\2</a>", $text);

i tried making it get the title of the link to display but it becomes vary laggy.
 
Ah, I got it now
Code:
	$message = preg_replace('|\[url="http\:\/\/(.+?)"\](.+?)\[\/url\]|i', '<a href="http://$1" target="_blank">$2</a>', $message);
	$message = preg_replace('|\[url="ftp\:\/\/(.+?)"\](.+?)\[\/url\]|i', '<a href="ftp://$1" target="_blank">$2</a>', $message);
	$message = preg_replace('|\[url="(.+?)"\](.+?)\[\/url\]|i', '<a href="http://$1" target="_blank">$2</a>', $message);

that seems to work. :D
 
Ah, I got it now
Code:
    $message = preg_replace('|\[url="http\:\/\/(.+?)"\](.+?)\[\/url\]|i', '<a href="http://$1" target="_blank">$2</a>', $message);
    $message = preg_replace('|\[url="ftp\:\/\/(.+?)"\](.+?)\[\/url\]|i', '<a href="ftp://$1" target="_blank">$2</a>', $message);
    $message = preg_replace('|\[url="(.+?)"\](.+?)\[\/url\]|i', '<a href="http://$1" target="_blank">$2</a>', $message);
that seems to work. :D
It takes you three calls to do what could be done in one.

Not efficient enough, try harder :thumbup1:
 
Yeah, something like that. For what you want, you need to put 'http://' before any URLs that don't contain something already. You may want to put exceptions for https, and ftp. That involves a conditional statement on a callback or something clever in REGEX.

This below will assure their link contains ftp, http, or https.
PHP:
$message = preg_replace('#\[url\=((?:ftp|https?)://.*)\](.*)\[/url\]#si','<a href="\1" target="_blank">\2</a>',$message);
It doesn't protect from XSS injections, and also it doesn't add http if there isn't any protocol on the url.

So you still have some work to do, but at least it's a starting point. You may have to create a preg_replace_callback statement and a function to go with it.. Hopefully not though.. Unfortunately I don't have the time to do it right now :)
 
Last edited:
Yeah, something like that. For what you want, you need to put 'http://' before any URLs that don't contain something already. You may want to put exceptions for https, and ftp. That involves a conditional statement on a callback or something clever in REGEX.

This below will assure their link contains ftp, http, or https.
PHP:
$message = preg_replace('#\[url\=((?:ftp|https?)://.*)\](.*)\[/url\]#si','<a href="\1" target="_blank">\2</a>',$message);
It doesn't protect from XSS injections, and also it doesn't add http if there isn't any protocol on the url.

So you still have some work to do, but at least it's a starting point. You may have to create a preg_replace_callback statement and a function to go with it.. Hopefully not though.. Unfortunately I don't have the time to do it right now :)

Yes, but why would I need callback?
It's not like I'm calling a function or anything.

It doesn't protect from XSS injections, and also it doesn't add http if there isn't any protocol on the url.

I have yet to study up about XSS injections. (lol)
Is my current parser vulnerable?

EDIT:
http://www.testingsecurity.com/how-to-test/injection-vulnerabilities/XSS-Injection
Ahh...
How could an XSS attack occur from your parser?
 
Last edited:
i would say pass this function before it enters the parser.

PHP:
function htmlout($text) {
	$text = stripslashes($text);
	$text = htmlspecialchars($text);
	$text = nl2br($text);
	return $text;
}

I'm currently doing that, but kinda differently. :P
PHP:
function parseMessage($message)
{
	return parseBBCode(nl2br(strip_tags(stripslashes($message))));
}
 
An XSS injection can occur anytime a user has the right to put content on your site, and they are given the ability to place HTML or JavaScript on your site that can cause malicious harm to your site.

The very reason BBcode was invented was to give the user the ability to put mark-up on a site, without the ability to use harmful JavaScript or unwanted tags.

An example of an XSS injection would be some JavaScript that redirected away from your site, or maybe it might break some of the HTML tags so the page doesn't display as intended, or they can completely high-jack your site if severe enough.

If it gets to the point where an XSS injection can effect the database, there are really huge problems, beyond XSS.

A few posts back I explained how my BBcode let an XSS injection through in rare situations. You might look back at that.


Edit:
@you & holthelper
Don't use strip_tags if you want to use things like [ code] or [ php] tags. I'd suggest using htmlentities(), personally. htmlspecialchars.. It's a good function, but I don't prefer it.
 
Last edited:
Back