String comparison in several parts

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

String comparison in several parts

Post 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 :-)
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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

:-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

|| means OR and && means AND
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

or, you can try similar_text()
Post Reply