Forum Moderators: coopster

Message Too Old, No Replies

Find an expression in a text

test if there is one

         

ticatoc

6:36 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Hello,

I've a text with an expresssion and I want to test if there is a specific expression in this text.

For example :

$text = 'blahblahblibli';
if (?) // if there is "blahblah" in $text
{
do this ();
}

So, I'm looking for a php function to test if there is an expression in a text. I don't know it but that might exists, isn't it :-?

vincevincevince

6:37 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



strpos() [php.net] is your friend for this. Take care to use === with it not ==!

ticatoc

7:01 am on Sep 25, 2007 (gmt 0)

10+ Year Member



This is supposes to work, isn't it :

$text = 'testblahtestii';
$findme = 'blah';
if (strpos($text, $findme) === TRUE ) {
echo $text;
}

:-?

I don't understand why there is nothing :-?

Habtom

7:04 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$pos = strpos($text, $findme);
if ($pos === TRUE ) {
echo $text;
}

ticatoc

7:10 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Huh,

If I make this :

if ($pos === false) {
}
else {
echo "OK!";
}

that work, but if I make this that doesn't :

if ($pos === true) {
echo "OK!";
}
else {

}

Isn't it weird?

vincevincevince

7:12 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that work, but if I make this that doesn't :

if ($pos === true) {
echo "OK!";
}
else {

}

Isn't it weird?

Not weird. strpos() returns false if the string is not found, and the character number of the start of the substring if it is found.

Assuming the string is found, then there will be some number returned (not true), so:

$pos === true << fails

What you mean is:

if ($pos!== false)

ticatoc

7:17 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Wow, ok I see it thanks to you vince :-)!