/    Sign up×
Community /Pin to ProfileBookmark

How to do a search box ?

I want to do a search box that can find
$word in $sentence

so if $word = “cuff” and
$sentence = “Gold cuff links”

then the if stmt will be true.

Do I use a LIKE stmt or something ?

i.e.

if ( $word LIKE $sentence ){
do it
}

Thanks for any help !

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@BeachSideFeb 14.2005 — you could always use a regex

[code=php]
<?php
$word = "cuff";
$sentence = "the gold cuff link shines";
if (preg_match('/\b' . $word . '\b/', $sentence)) {
echo "success";
} else {
echo "fail!";
}
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogFeb 14.2005 — [i]Originally posted by BeachSide [/i]

[B]you could always use a regex



[code=php]
<?php
$word = "cuff";
$sentence = "the gold cuff link shines";
if (preg_match('/\b' . $word . '\b/', $sentence)) {
echo "success";
} else {
echo "fail!";
}
?>
[/code]
[/B][/QUOTE]

To make it case-insensitive:
[code=php]if (preg_match('/\b' . $word . '\b/i', $sentence)) {[/code]
Alternatively, if you prefer that "cuff" matches "the gold cuff link shines" and "police use handcuffs sometimes", you could just use:
[code=php]
# importatnt that you use !== and NOT !=
if(strpos(strtoupper($sentence), strtoupper($word)) !== FALSE)
{
# found it
}
else
{
# did not find it
}
[/code]
Copy linkTweet thisAlerts:
@DaveinLondonauthorFeb 15.2005 — Thanks -

i guess the LIKE is in some other laguage ?

maybe js ? I saw it somewhere.
Copy linkTweet thisAlerts:
@NogDogFeb 15.2005 — [i]Originally posted by DaveinLondon [/i]

[B]Thanks -

i guess the LIKE is in some other laguage ?

maybe js ? I saw it somewhere. [/B]
[/QUOTE]

It's used in SQL.
×

Success!

Help @DaveinLondon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...