/    Sign up×
Community /Pin to ProfileBookmark

preg_match from entire document

I’ve got a textarea form post which I’m processing.

In this post will be an article which will likely have multiple names in it. First Name – Last Name, Last Name First Name mixture.

I want to do a preg_match (I believe) on all my students to find all matches.

My students is and object list with firstname and lastname seperated in two properties.

Would I need to do two preg_matches for all students in my list against the entire document?

Any better ways of doing this?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 13.2021 — It's going to depend on how many different ways you want to match. If you have first name "John" and last name "Smith" for one student, which of these would you match on?
  • * John Smith

  • * john smith

  • * J. SMITH

  • * Smith, John

  • * John W. Smith

    etc...


  • But if you can assume (or at least settle) for it always being "first_name last_name", I might just search for each using strpos() (or stripos() if you want to be case-insensitive). Remember, though, that it returns 0 if the string is matched at the very beginning of the text, so you want to test something like:
    [code=php]
    $name = "$first_name $last_name";
    if(strpos($text, $name) !== false) {
    // found it
    }
    [/code]
    ×

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