Page 1 of 1

String comparison in several parts

Posted: Sun Apr 04, 2004 11:17 pm
by Chris Corbyn
Hi,

How can you compare two strings to see if any part of the string matches even if it is separated by paces or extra characters etc. Sorry i can't really put into words what i need to do so I'll give an example.

Say you've got two variables:

Code: Select all

<?php
$variable1 = "The quick brown fox jumped over the fence";
$variable2 = "jumped fox";
?>
strpos($variable1, $variable2); only finds the the position in the string if $variable2 is just "fox" but not those two words in the wrong order etc.

Am I getting somewhere near to explaining what i need to do here?

Basically, I want an

Code: Select all

<?php
if(/*some string function*/($variable1, $variable2) !== false) {
         /*Do Some thing*/
} else {
         /*Do other*/
}
?>
And if I was using those two variables I need it to /*Do Some thing*/.

Is there a string function which will do this or am I fighting a losing battel here?

Case insensitive would be great too!

Thanks :-)

Posted: Mon Apr 05, 2004 12:03 am
by Paddy
Write a function. One that takes the string to be searched and an array of strings that you want to search for. If it finds them all in the searchable string then it returns true else it returns false.

Posted: Mon Apr 05, 2004 12:19 am
by Chris Corbyn
When you say an array of strings that i want to search it for, do you mean by doing something like breaking the string up at every space (" ") using the explode function or something along thos lines.

I've had a play around doing that but I'm quite new to php so i was struggling to get it to return any results at all.

I'll play around with it a little longer before I get someone else to take a look though. I prefer to understand the code than just use some ready written code.

Posted: Mon Apr 05, 2004 12:27 am
by Chris Corbyn
It's ok thanks I've worked it out. I just explode the variable to create an array and then check for any of the values in the array in the string.

Thanks for pointing me in the right direction

:-)

Posted: Mon Apr 05, 2004 12:57 am
by Chris Corbyn
I have another question just to brush something up on this topic.

I've worked out the code i need but I'm using || to include several possiblities. However this notation does not mean that all possibilities must happen simultaneously. What is the equivalent of || to say AND instead of OR?

Posted: Mon Apr 05, 2004 1:03 am
by andre_c
|| means OR and && means AND

Posted: Mon Apr 05, 2004 7:06 am
by magicrobotmonkey
or, you can try similar_text()