473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Checking if a string contains a word?

Markus
6,050 Expert 4TB
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
Sep 22 '07 #1
7 11613
bergy
89
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.
Sep 22 '07 #2
Markus
6,050 Expert 4TB
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.
Sep 22 '07 #3
bergy
89
Ahh!

Thanks man :D

Or woman!

Cheers, again.
Haha - No problem - and I'm all man baby... yeah!
Sep 22 '07 #4
Markus
6,050 Expert 4TB
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.
Sep 23 '07 #5
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]
Sep 24 '07 #6
kovik
1,044 Expert 1GB
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]
Sep 24 '07 #7
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...
Sep 24 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nel | last post by:
Hi all, Before I re-invent the wheel here, has anyone willing to share a basic script to extract META keywords from a string. I have a string, let's say $pageText that contains the dynamic...
3
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed...
5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
3
by: Vish | last post by:
Hello, I am passing a string to a spell checking function which performs spell check using word objects. It works fine but even though i have said oWord.Visible = false; the word application...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
2
by: bdog4 | last post by:
I've got a spell checking function in php that will check the words in a sentance and suggest corrections. Right now I have it to print out the words with 6 suggestions for each misspelled word. ...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
3
by: OKB (not okblacke) | last post by:
I'm trying to write a unicode raw string literal, and I seem to be running up against a conflict between the \uXXXX unicode character escape and the need to have a literal \u (i.e., backslash...
10
by: Brent | last post by:
I have a list of company names (say, IBM, Corning, General Motors, and another 5,000 of them). If I take a body of text, a news article, for instance, and I want to see which company names...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.