...

View Full Version : search string for a phrase



DesignersToolz
04-02-2004, 12:38 AM
How do I check if a string contains a phrase? I'm looking for INVALID in a string. What I want to do exacly is if a string contains INVALID then it erases everything but the INVALID

example:

88fdfdINVALIDegr // becomes INVALID

INVALID // becomes INVALID

randomtextINVALID // becomes invalid



thanks.. :cool:

raf
04-02-2004, 11:48 AM
Nobody responds?? Isn't it just something like


$tringtosearchandtransform = '88fdfdINVALIDegr';
if (stristr('INVALID', $tringtosearchandtransform) !== FALSE ){
$tringtosearchandtransform = 'INVALID';
}
echo $tringtosearchandtransform;

(case sensitive)

DesignersToolz
04-02-2004, 05:45 PM
Close but no dice :(

DesignersToolz
04-02-2004, 06:07 PM
OK, I figured this out myself. I used preg_match();



<?php
$string = "LALAlalala7r64INVALD373";
if (preg_match("/INVALID/i", $string))
{
$string = "INVALID";
}
else
{
}
print $string;
?>

dswimboy
04-02-2004, 06:10 PM
yeah, don't forget that you're adding the /i switch to the RegEx. this makes the search case-INsensitive.

it will match "Invalid", "InVaLiD", or any other messed up stuff you can think of.

raf
04-02-2004, 06:58 PM
Close but no dice :(
:o Yess, should have been


if (stristr($tringtosearchandtransform, 'INVALID') !== FALSE ){
$tringtosearchandtransform = 'INVALID';
}

PHP isn't realy consistent about the haystack-needle attributes order

DesignersToolz
04-02-2004, 07:04 PM
I tried reversing the paremeters once but with no luck. ( I had changed some other things to so..)

raf
04-02-2004, 07:48 PM
I tried reversing the paremeters once but with no luck. ( I had changed some other things to so..)
:confused:
Just tried it and it worked. Anyway, there are about a dozen ways to do this so take your pick.

1stzip
04-02-2004, 08:54 PM
<?php
$input_string='akfhi758q2 345kINVALIDjksey5829';
$string = 'INVALID';
$checkstring = strstr($input_string, $string);
if(strlen($checkstring)!=0)
{echo $string;
}

?>

DesignersToolz
04-02-2004, 09:31 PM
Just tried it and it worked. Anyway, there are about a dozen ways to do this so take your pick.



Thats why I said i had changed some other things to heh. ;)

missing-score
04-02-2004, 11:31 PM
I would use:


<?php
if(stripos($string, 'invalid') !== false){
$string = 'INVALID';
}
echo $string;
?>

but like raf said there are several ways of doing it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum