Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:06:17+00:00 2026-05-26T02:06:17+00:00

I have a function in php which I would like to perform a simple

  • 0

I have a function in php which I would like to perform a simple search on a string, using a kw as the search phrase, and return true if found.

This is what I have now:

for($i=0; $i<count($search_strings); $i++){
   $pos = strpos($search_strings[$i], $kw_to_search_for);
}

This works fine, and does actually find the Keyword inside the string beeing searched, but the problem is that strpos doesn’t match exact phrases or words.

For instance, a search for ‘HP’ would return true if the word ‘PHP’ was in the string.

I know of preg_split and regular expressions which can be used to do exact matches, but in my case I don’t know what the keyword is for every search, because the keyword is user-inputted.

So the keyword could be “hot-rods”, “AC/DC”, “Title:Subject” etc etc…
This means I cannot split the words and check them separately because I would have to use some kind of a dynamic pattern for the regex.

If anybody know of a good solution I would much appreciate it.

I mean, basically I want exact matches only, so if the KW is “Prof” then this will return true if the match in the searched string is “Prof” and doesn’t have any other characters surrounding it.
For instance “Professional” would have to be FALSE.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:06:17+00:00Added an answer on May 26, 2026 at 2:06 am

    You can use word boundaries \b:

    if (preg_match("/\b".preg_quote($kw_to_search_for)."\b/i", $search_strings[$i])) {
        // found
    }
    

    For instance:

    echo preg_match("/\bProfessional\b/i", 'Prof'); // 0
    echo preg_match("/\bProf\b/i", 'Prof');         // 1
    

    /i modifier makes it case insensitive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function from which I would like to return a value as
I'm not very good at PHP and would like to have a PHP function
I have a function that I use on index.php page and I would like
i would like to know if it is possible to have a function in
Within a wordpress plugin, I have a PHP function which transform $file_name (a CSV
I have programmed a proxy.php script (listed below), which would fetch an image specified
I have a cookie which I would like to read then remove one entry
I have a jQuery call to some PHP which works fine. What I would
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
I have a JavaScript function, pop_item . I have to call this from PHP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.