Need some help with matches

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Need some help with matches

Post by JKM »

Hi there,

I wan't to check if $_GET['lol'] is beginning with ROFL* or not.

Code: Select all

if($_GET['lol'] starts with ROFL) {
    $result = "LMAO";
}
else {
    $result = "Get lost";
}
The question is how I can check this.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Need some help with matches

Post by socket1 »

something like

Code: Select all

 
if(strpos($_GET['lol'], "ROFL") == 'int') {
echo "Found ROFL";
}
else
{
echo "No ROFL";
}
 
That code worked for me.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need some help with matches

Post by requinix »

socket1 wrote:That code worked for me.
Uh... really?

Code: Select all

if (strncmp($_GET["lol"], "ROFL", 4) == 0) {
    // starts with ROFL
} else {
    // doesn't start with ROFL
}
If you use str[n]cmp it'll only look at the start of the search string and not scan the entire thing looking for a match.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Need some help with matches

Post by socket1 »

Oh ok, thanks, didn't know that.
Post Reply