Welcome Guest, Not a member yet? Register   Sign In
find a string...
#1

[eluser]outtolunch[/eluser]
Hello guys,

I'm trying to find a string in the other string. I've used strpos and preg_match, it works fine, but there is one problem.

For example:

Code:
$haystack='This is a red car';
$needle='green car';
$check = strpos($haystack, $needle);

if($check==true){
echo 'Found';
}
else {
'Not found';
}

it will return true, as it found "car" in the string, but i don't want that, as it really should look for "green car" and as it's red, should return "false". The same thing is with preg_match, I've tried them both and experimented a bit.

Any suggestions of how to look for exact string within the other string? That would be very helpful, thanks!
#2

[eluser]outtolunch[/eluser]
I'm stuck down here, been trying to find the answer. Any suggestions?
#3

[eluser]outtolunch[/eluser]
Ok, i managed to work this out i think.

Maybe my question wasn't the best. So yeah, for example i have some records in the database-> car, red car, green car.

The string is-> "I have got a red car."

I want to check the records against the string and find out what i've got in that string.

So i run the loop, i check "car" against the string then "red car" and then "green car". By the way i'm using strpos();

So the system will find two matches, "car" and "red car". I recorded those results into an array, and displayed the last record of the array, which is "red car".

It does't really sound as the best way of doing it, but it works for me. Of course if you have a table with records laid like that-> "red car", "car" I would pull out car out of the array, which wouldn't be accurate.

Uhh....

Feel free to comment.
#4

[eluser]WanWizard[/eluser]
Don't know which PHP you're using, but here your example returns FALSE.

With strpos() you have to be careful anyway, because strpos('this is a string', 'this') returns 0 (zero), not FALSE. The best way to deal with this is:
Code:
$haystack='This is a red car';
$needle='green car';
$check = strpos($haystack, $needle);

if (  $check===FALSE )
{
    echo 'Not found';
}
else
{
    echo 'Found';
}
#5

[eluser]outtolunch[/eluser]
The thing is, that it first checks car and then red car, they both will return true, but i still need red car.

Thanks for your reply Smile
#6

[eluser]jedd[/eluser]
[quote author="outtolunch" date="1274056334"]The thing is, that it first checks car and then red car
[/quote]

What?

How?

Quote:... they both will return true, but i still need red car.

Either PHP is fundamentally broken, or you're not describing your code properly / fully.
#7

[eluser]vitoco[/eluser]
[quote author="outtolunch" date="1274056334"]The thing is, that it first checks car and then red car, they both will return true, but i still need red car.

Thanks for your reply Smile[/quote]

strpos is a short for "string position", so it doesn't return true or false in case that the "needle" string it's found in the "haystack" string, it only returns the position (int) of the needle in the haystack. so the value goes from 0 ( zero) to strlen( $haystack) - 1 .

the correct way
Code:
$haystack = 'This is a red car';
$needle   = 'green car';
$position = strpos( $haystack , $needle );

// POSITION INT 0....N-1
if( $position !== false )
{
    echo 'Found';
}
// POSITION FALSE
else
{
    echo 'Not found';
}

Saludos




Theme © iAndrew 2016 - Forum software by © MyBB