Jump to content

Check if word exist


Go to solution Solved by cataiin,

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

Edited by ankur0101
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.