Can you search for a word in a variable - randomly?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you search for a word in a variable - randomly?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Can you search for a word in a variable - randomly?

Post by JakeJ »

look up preg_match()
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you search for a word in a variable - randomly?

Post by simonmlewis »

Excellent - got it working.

Thanks very much.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Can you search for a word in a variable - randomly?

Post by JakeJ »

Glad I could help.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can you search for a word in a variable - randomly?

Post by AbraCadaver »

strpos() is faster and easier:

Code: Select all

if(strpos($row->title, "DOG") !== false)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can you search for a word in a variable - randomly?

Post by pickle »

AbraCadaver wrote:strpos() is faster and easier:

Code: Select all

if(strpos($row->title, "DOG") !== false)
+1
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you search for a word in a variable - randomly?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can you search for a word in a variable - randomly?

Post 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.")) {
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you search for a word in a variable - randomly?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Can you search for a word in a variable - randomly?

Post 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 :D
Post Reply