/    Sign up×
Community /Pin to ProfileBookmark

word or phrase in string

I’ve got a bunch of headlines “The quick brown fox jumped over the lazy dog” just as a single example, but I’ll be looping through an array.

What I want to do it check if the headline has any relevance to me.

Firstly I was going to do a in array but that means my headline and word list need to both be arrays and I don’t think i’ll be able to do multi work searches?

str_contains is an option in PHP8, but I can’t upgrade that yet.

Any good ideas?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 01.2021 — I'd probably look at using one of the [PCRE functions](https://www.php.net/manual/en/ref.pcre.php), with the specific choice likely depending on how many headlines and how many keywords you'd want to search at one time. It takes some forethought with the terms you want to search for, as for instance you might want to match on both "fox" and "foxes", but don't want to match "Firefox"; so you might search for the regex '/bfox(es)?b/i' (to match either the word "fox" or "foxes" or "Fox" or "FOXES").
Copy linkTweet thisAlerts:
@kiwisauthorMay 02.2021 — I like what you've suggested above, it may solve this current issue

If my headline has a double work which should be a match, as below. It returns false.

What can I do here? I can't add a preg match for each item in my phrase array

``<i>
</i>$phrase = array('Denver Broncos', 'NFL', 'Touchdown', 'Super Bowl');
foreach ($phrase as &amp;$item){
$pos = strpos($headline, $item);
if ($pos == true) {
echo "&lt;b&gt;" . $headline . "&lt;/b&gt;&lt;br&gt;";
} else {
echo "Not true&lt;br&gt;";
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@kiwisauthorMay 03.2021 — I've not stripped the spaces out inside the foreach loop and setting $headline to

The Denver Broncos win the 2022 Super Bowl

But I'm getting not true.


``<i>
</i>
$headline = str_replace(" ", "", $headline);
$item = str_replace(" ", "", $item);<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogMay 03.2021 — How about something like:
[code=php]
$phrases = array('Denver Broncos', 'NFL', 'Touchdown', 'Super Bowl');
$regex = "/b(" . implode('|', $phrases) . ")b/i";
echo $regex;
// /b(Denver Broncos|NFL|Touchdown|Super Bowl)b/i
$headline = "The Denver Broncos win the 2022 Super Bowl";
$hits = preg_match_all($regex, $headline);
echo $hits;
// 2
[/code]
Copy linkTweet thisAlerts:
@kiwisauthorMay 03.2021 — Should this find matches like Denver Wildcats?
Copy linkTweet thisAlerts:
@NogDogMay 03.2021 — > @kiwis80#1631111 Should this find matches like Denver Wildcats?

No, it would look explicitly for "Denver Broncos" in my example (regardless of case, by the way (the final /i at the end of the regex)).

Depending on how the headlines are formatted, you might want to substitute s+ for any spaces, so that it would match on any number of white-space characters (e.g. newlines as well as spaces).
×

Success!

Help @kiwis 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 3.28,
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: @darkwebsites540,
tipped: article
amount: 10 SATS,

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

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...