/    Sign up×
Community /Pin to ProfileBookmark

Loop & Find Matches

I have a large body of text in a variable called $storyText. It’s paragraphs of text in a news story.

I want to loop through an array of people in my database and see if there’s a match. Sometimes the story will be “firstname lastname”” other times it’ll be “knownas lastname” other times it’ll be first “lastname firstname”.

I already have a list of names in my array. I was going to do something like this.

Within each if statement, just a particular name arrangement. I have properties, firstname, lastname and knownas within the person object.

My question is.. what’s the best way to match each name string to something in my $storyText?

preg_match or something else?

[code]
$matchedIDs = array();
foreach($personList as $person){
if(){

$matchedIDs[] = $person->getID();
} elseif (){
….
$matchedIDs[] = $person->getID();
} elseif(){
….
$matchedIDs[] = $person->getID();
}

}
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorDec 28.2021 — Here's what I tried

getFullName method, returns "FirstName LastName"

I have names that should match but I'm not getting any results returned in my matchedIDs array

<i>
</i> foreach($personList as $Person){
if(strpos($text, $Person-&gt;getFullName()) === true){
$matchedIDs[] = $Person-&gt;getPersonID();
}
}
Copy linkTweet thisAlerts:
@NogDogDec 28.2021 — strpos() returns an integer if a match is found (the position in the string, which could be 0 if at the start), and Boolean false if not found, so change your comparison to !== false.
×

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 4.26,
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,
)...