Jump to content

Check if word exist


ankur0101

Recommended Posts

Hi,

I am really bad when it comes to Regex. Every time, when I need to do things with it, I get confuse.

I want to find a word called hello in a variable. something like,

$word = "hello";

$target = "hello_guys_howz_life";

Using preg_match, how can I check whether hello word exist in variable $target ?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/282285-check-if-word-exist/
Share on other sites

with preg_match:

if(preg_match("/$word/", $target))
{
    echo "Exists.";
}
else
{
    echo "Nop.";
}

sorry but then I saw you want with preg_match.

 

Yes, but try and steer him in the direction of not using regex if it is not needed. As you know strpos() will be much faster than preg.

Link to comment
https://forums.phpfreaks.com/topic/282285-check-if-word-exist/#findComment-1450271
Share on other sites

I think saying something would "utterly fail" at something it isn't designed to do in the first place is being a bit harsh and misleading. It's like saying a fish would utterly fail at flying. Well it's not built for the sky in the first place, so if anything is failing, it's your own expectations and/or understanding of it.

Link to comment
https://forums.phpfreaks.com/topic/282285-check-if-word-exist/#findComment-1450301
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.