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 8922929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:58:24+00:00 2026-06-15T06:58:24+00:00

I am working with some code in PHP that grabs the referrer data from

  • 0

I am working with some code in PHP that grabs the referrer data from a search engine, giving me the query that the user entered.

I would then like to remove certain stop words from that string if they exist. However, the word may or may not have a space at either end.

For example, I have been using str_replace to remove a word as follows:

$keywords = str_replace("for", "", $keywords);
$keywords = str_replace("sale", "", $keywords);

but if the $keywords value is “baby formula” it will change it to “baby mula” – removing the “for” part.

Without having to create further str_replace’s that account for ” for” and “for ” – is there a preg_replace type command I could use that would remove the given word if it is found with a space at either end?

My idea would be to put all of the stop words into an array and step through them that way and I suspect that a preg_replace is going to be quicker than stepping through multiple str_replace lines.

UPDATE:
Solved thanks to you guys using the following combination:

$keywords = "...";
$stopwords = array("for","each");
foreach($stopwords as $stopWord)
{
    $keywords = preg_replace("/(\b)$stopWord(\b)/", "", $keywords);   
}
  • 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-06-15T06:58:25+00:00Added an answer on June 15, 2026 at 6:58 am
    $keywords = "...";
    $stopWords = array("for","sale");
    foreach($stopWords as $stopWord){
        $keywords = preg_replace("/(\b)$stopWord(\b)/", "", $keywords);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code that reads from a socket, and it goes wrong
I am working on some code that I inherited from a programmer who is
I'm working on porting some PHP code to C, that contacts a web API.
I have some PHP code that was at one point WORKING FINE. It makes
I'm working on some PHP code (that I didn't write). Here and there inside
This is some code from a php file im working with. I need to
I am working on some legacy PHP code that holds a ton of in-line
I am working on some PHP code currently that seems to work logically $tod
I'm working with some old PHP code that has a lot of the following:
I have been working on some code to submit an uploaded image to 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.