• 0

PHP regular expression


Question

I've added a little bit of code to my vbulletin forum to show error if the signature contains .gif, however it only works if the signature only contains .gif, if it contains anything else it fails.

So i believe i need to add a regular expression to fix this so that it will pick up any text before the .gif

if ($signature == ".gif")

how can i implement a regular expression into this so that it will pick up all text before .gif, a typical link would be something like

http://www.somewebsite.com/images/animated.gif

so i need a reg expression for the part in bold

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You could do this with a regular expression, however there's an even better method using PHP's parse_url and pathinfo functions.

<?php
$url = "http://www.somewebsite.com/images/animated.gif";
$extension = strtolower( pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_EXTENSION ) );
if($extension == "gif") {
    // This is a GIF image 
}
?>

First, it calls parse_url to retrieve the path component from the URL, this is the part after the domain and before the query. Then, pathinfo is called to retrieve the file extension out of the path component. Lastly, the result is converted to lower-case so you can do case-insensitive matching. The advantage of using this over a regular expression is that it works for any given URL. It can have a query (e.g. "?p=home") or a fragment (e.g. "#top") after the actual file name, parse_url handles it easily whereas a regular expression could become much more complicated.

Link to comment
Share on other sites

  • 0

Thanks for that, i have come up with this and it seems to work:


if (preg_match('/.gif/', $signature))
	{
		$errors[] = fetch_error('disabledword');
	}

It's quite a basic need so the advantage of using your example doesn't really apply? (i'm not knocking it, just interested to know:)...)

Link to comment
Share on other sites

  • 0

Thanks for that, i have come up with this and it seems to work:

 if (preg_match('/.gif/', $signature)) 	{ 		$errors[] = fetch_error('disabledword'); 	} 

It's quite a basic need so the advantage of using your example doesn't really apply? (i'm not knocking it, just interested to know:)...)

Unfortunately, your pattern will match any string with .gif in it. Such as this.was.a.gif.jpeg which isn't desired behaviour. You may as well use stripos to find it if so.

If you're dead set on using RegEx for this, you really want to be looking at something like..

<?php
if(1 === preg_match('~.gif$~i', $filename)){
 #gif found
}
?>

This is case-insensitive and only matches .gif at the end of the given string.

Link to comment
Share on other sites

  • 0

Unfortunately, your pattern will match any string with .gif in it. Such as this.was.a.gif.jpeg which isn't desired behaviour. You may as well use stripos to find it if so.

If you're dead set on using RegEx for this, you really want to be looking at something like..

<?php
if(1 === preg_match('~.gif$~i', $filename)){
 #gif found
}
?>

This is case-insensitive and only matches .gif at the end of the given string.

using that allowed http://www.somesite.com/images/image.gif

but disallowed it without the http://

Link to comment
Share on other sites

  • 0

what stops me from using a tinyurl or something. Or using something like mod_rewrite to hide the .gif extension....

i don't think my board users are that smarttongue.gif most will simply follow the rules, this is just a bit of extra to back it up.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.