/    Sign up×
Community /Pin to ProfileBookmark

I’ve never got my head around regular expressions.

Here’s what I have, I have a word, say Honda.

I’m going through an array and watch to find matches?

What I want to find though, it matches which are from ”Hond’ – So partial matches?

Is it possible to also get matches from ”Honder’ – I thought preg_match gave a percent comparison? _

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@boohooOct 10.2021 — Keep in mind that in PHP 8 there's a str_starts_with() function.

Before PHP 8, you could also check if the string starts with something with strpos():

``php<i>
</i>$myString = 'Honder the bonder';
$prefix = 'Hond';
if (strpos($myString, $prefix) === 0) {} // 0 means it starts with the prefix<i>
</i>
`</CODE>

That being said, sometimes you need regular expressions, obviously. You should have given a precise example of what you're trying to do, but from what I got from your question, you're looking for something like this:

<CODE lang="php">
`php<i>
</i>$entries = [
'boo',
'Honduras',
'hoo',
'hOnDa',
];

$matches = array_filter(
$entries,
function ($entry) {
return preg_match('#^hond#i', $entry);
}
);

// $matches is [1 =&gt; 'Honduras', 3 =&gt; 'hOnDa']<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogOct 10.2021 — > @boohoo#1638026 You should have given a precise example of what you're trying to do

Yeah, the solution may change based on whether you only want to match to the start of the string or anywhere in the string, if it is/isn't case sensitive, what you want to do with white space and/or punctuation, etc. Once you nail down the exact requirements, it can really narrow down the approach to take on it.
Copy linkTweet thisAlerts:
@kiwisauthorOct 10.2021 — I was looking for similar_text function!

Although, this has lead me into another enhancement. I'm comparing strings against a name field. Honda for example, I'm ranking results based on the match percentage. It works well.

The issue is, a user may search Honda Civic Sedan, in my JSON file, I have name, model and style.

I can match all 3 against all fields but where I get multiple matches I want to rank results higher.

I was thinking of splitting the search string into individual words as an array. Compare them against each field in my JSON. Where I have a results over 75% I score the record.

Matched records go back into a master results array. Which I sort by this ranking

So if I search with 3 words, each matches with 88% my score is 3. Searching just Honda with 100% match scores 1 and comes after.

Thoughts?
×

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