Page 1 of 1
Can you search for a word in a variable - randomly?
Posted: Thu Jul 08, 2010 3:23 am
by simonmlewis
Code: Select all
$friend= substr("$row->title", -8);
if ($friend== "DOG")
{ echo "
Hello. This code is meant to go back 8 characters and find the word DOG. It is the 8th character, but I was wondering if there is a way to search the entire variable, randomly (ie. 4th, 5th.... ANY set of characters) for a particular word.
I can't find any method to do this.
Re: Can you search for a word in a variable - randomly?
Posted: Thu Jul 08, 2010 3:44 am
by JakeJ
look up preg_match()
Re: Can you search for a word in a variable - randomly?
Posted: Thu Jul 08, 2010 3:47 am
by simonmlewis
Excellent - got it working.
Thanks very much.
Re: Can you search for a word in a variable - randomly?
Posted: Thu Jul 08, 2010 10:25 am
by JakeJ
Glad I could help.
Re: Can you search for a word in a variable - randomly?
Posted: Thu Jul 08, 2010 10:41 am
by AbraCadaver
strpos() is faster and easier:
Code: Select all
if(strpos($row->title, "DOG") !== false)
Re: Can you search for a word in a variable - randomly?
Posted: Fri Jul 09, 2010 10:34 am
by pickle
AbraCadaver wrote:strpos() is faster and easier:
Code: Select all
if(strpos($row->title, "DOG") !== false)
+1
Re: Can you search for a word in a variable - randomly?
Posted: Fri Jul 16, 2010 4:41 am
by simonmlewis
JakeJ wrote:look up preg_match()
Code: Select all
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
How can you use this, but look for several words? ie.
to look for "http", "@", ".co.uk"... etc?
Re: Can you search for a word in a variable - randomly?
Posted: Fri Jul 16, 2010 10:55 am
by AbraCadaver
You can try the OR operator | :
Code: Select all
if (preg_match("/php|http|@|\.co\.uk/i", "PHP is the web scripting language of choice.")) {
Re: Can you search for a word in a variable - randomly?
Posted: Fri Jul 16, 2010 12:50 pm
by simonmlewis
Thanks - that's works a treat.
Is there a way to reverse the query?
Say, if it does NOT have "this, that and that" in the variable, then do this?
EDIT>..... actually, i think you have to say "if it has this this and that, then run the query - else, don't.
Re: Can you search for a word in a variable - randomly?
Posted: Fri Jul 16, 2010 3:21 pm
by shawngoldw
I think you can use the not operator ^
Code: Select all
if (preg_match("/[^php|http|@|\.co\.uk]/i", "PHP is the web scripting language of choice.")) {
I just started to learn regular expressions like 2 days a go though
