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

  • SEARCH
  • Home
  • 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 9266971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:26:29+00:00 2026-06-18T14:26:29+00:00

I am trying to find certain words in a string and replace them with

  • 0

I am trying to find certain words in a string and replace them with links to pages

I have three arrays which are like this (this is just an example not the actual thing :P)

$string = "Oranges apples pears Pears <p>oranges</p>";
$keyword = array('apples', 'pears', 'oranges');
$links = array('<a href="apples.php">Apples</a>', '<a href="pears.php">Pears</a>', '<a href="oranges.php">Oranges</a>');
$content = str_replace($keyword, $links, $string);
echo $content;

It replaces some of the words but not all of them, that is because there are spaces infront of some words and at the end of some words and some are capitalised etc.

I was wondering what is the best way to achieve what I am trying to do. I also tried preg_replace but I am not too good with regex.

  • 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-18T14:26:30+00:00Added an answer on June 18, 2026 at 2:26 pm

    Simply use str_ireplace:

    $string = "Oranges apples pears Pears <p>oranges</p>";
    $keyword = array('apples', 'pears', 'oranges');
    $links = array('<a href="apples.php">Apples</a>', '<a href="pears.php">Pears</a>', '<a href="oranges.php">Oranges</a>');
    $content = str_ireplace($keyword, $links, $string);
    echo $content;
    

    There should be no problem with spaces. for str_replace it doesn’t if there is a space before/after the search term or not.

    If you only want to replace whole words then you need to use an regular expression:

    $string = "Oranges apples pear Pears <p>oranges</p>";
    $keyword = array('apples', 'pear', 'oranges'); // note: "pear" instead of "pears"
    $links = array('<a href="apples.php">Apples</a>', '<a href="pears.php">Pears</a>', '<a href="oranges.php">Oranges</a>');
    $content = preg_replace(array_map(function($element) {
        $element = preg_quote($element);
        return "/\b{$element}\b/i";
    }, $keyword), $links, $string);
    echo $content;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to find processes on 3 terminal servers which have certain words in
I'm trying to recursively find files of multiple types, and replace a certain string
I am trying to find words and replace them using regex. I keep getting
Trying to replace certain words in HTML pages with the same word but as
I have a lot of nested dictionaries, I am trying to find a certain
I have a web application project. I am trying to find out why certain
I am trying to find all the digits following a certain String pattern using
I'm trying to find certain keywords in a string with python. The string is
I have a list of keywords I'm trying to find in a text string.
I am trying to have this code run and delete a certain record in

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.