Connecting Tech Pros Worldwide Forums | Help | Site Map

Checking if a string contains a word?

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#1: Sep 22 '07
I've made a download page for my friends website.

The download is selected from the $_GET

But there are multiple uses of the page, such as;

downloads of .rars and .zips,

and viewable videos etc.

I would like to know if there's a way i can check what is passed from the $_GET['id']

To show whether it is a .rar or .flv, for example, so appropriate action can be taken to embed the .flv file instead of it showing a download link to the .flv

Example of urls
Expand|Select|Wrap|Line Numbers
  1. www.mahcuz.com/wltg.php?id=somefile.rar
  2. www.mahcuz.com/wltg.php?id=somefile.flv
  3.  
Sorry if i'm not precise enough, i can elaborate more if needed.

thanks a bunch :D

bergy's Avatar
Member
 
Join Date: Mar 2007
Posts: 90
#2: Sep 22 '07

re: Checking if a string contains a word?


Markus,

You want to use the strpos() function. If the specified string (needle) is in your "id" variable (haystack) it will return an integer of the starting position - if not, it will return a "false" value... so something like:

[php]
if(strpos(".flv", strtolower($_GET['id'])) != false){
echo "This is an FLV File!!";
}else{
echo "This is NOT an FLV file!!";
}
[/php]Note, you'll also want to use strtolower to make sure that capitalized file extnesions (.FLV) get included as well.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#3: Sep 22 '07

re: Checking if a string contains a word?


Quote:

Originally Posted by bergy

Markus,

You want to use the strpos() function. If the specified string (needle) is in your "id" variable (haystack) it will return an integer of the starting position - if not, it will return a "false" value... so something like:

[php]
if(strpos(".flv", strtolower($_GET['id'])) != false){
echo "This is an FLV File!!";
}else{
echo "This is NOT an FLV file!!";
}
[/php]Note, you'll also want to use strtolower to make sure that capitalized file extnesions (.FLV) get included as well.

Ahh!

Thanks man :D

Or woman!

Cheers, again.
bergy's Avatar
Member
 
Join Date: Mar 2007
Posts: 90
#4: Sep 22 '07

re: Checking if a string contains a word?


Quote:

Originally Posted by markusn00b

Ahh!

Thanks man :D

Or woman!

Cheers, again.

Haha - No problem - and I'm all man baby... yeah!
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#5: Sep 23 '07

re: Checking if a string contains a word?


Mmmmm it doesn't work.

It only says that it's an flv file if flv (with no period before) comes immediately after the id=

?id=flv

returns true

?id=something.flv

or

?id=.flv

both return false..

I'm lost.
karlectomy's Avatar
Member
 
Join Date: Sep 2007
Location: Vermont
Posts: 64
#6: Sep 24 '07

re: Checking if a string contains a word?


a better way to do this would be to use substr() which will take the last 4 chars into a new string. THis way, the file something.flv.exe won't be uploaded.

[PHP]
$substring = substr(strtolower($_GET['id'], -4));
if($substring == ".flv")
{
echo "This is an FLV File!!";
}
else echo "This is NOT an FLV file!!";
[/PHP]
kovik's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 828
#7: Sep 24 '07

re: Checking if a string contains a word?


Or even better, strrpos() would check from the end, so you could get the final extension using strrpos() and substr().

[php]if (($pos = strrpos($_GET['id'], '.')) != false) {
if ($pos < strlen($_GET['id'])) {
switch (strtolower(substr($_GET['id'], $pos + 1))) {
case 'flv':
echo "FLV";
break;
case 'zip':
echo "ZIP";
break;
}
}
}[/php]
karlectomy's Avatar
Member
 
Join Date: Sep 2007
Location: Vermont
Posts: 64
#8: Sep 24 '07

re: Checking if a string contains a word?


Quote:

Originally Posted by volectricity

Or even better, strrpos() would check from the end, so you could get the final extension using strrpos() and substr().

[php]if (($pos = strrpos($_GET['id'], '.')) != false) {
if ($pos < strlen($_GET['id'])) {
switch (strtolower(substr($_GET['id'], $pos + 1))) {
case 'flv':
echo "FLV";
break;
case 'zip':
echo "ZIP";
break;
}
}
}[/php]

Wait, I thought I just did that in 6 lines of code...
Reply