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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:45:35+00:00 2026-06-05T16:45:35+00:00

How can I replace all the occurrences of the $keyword within a string without

  • 0

How can I replace all the occurrences of the $keyword within a string without replacing the keywords found within the hyperlink URLs, image tag URLs, image tag title and alt tags?

Example:

$keywords = 'sports';

$string = '<a href="http://my_domain_name.com/sports/info.php"><img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" />Football sports news</a>';

Notice that the keyword ‘sports‘ appears with the hyperlink URL, images tag URL, and image tag title and alt tags.

I want to replace $keywords (sports) with:

<span style="color: #000000; background-color: #FFFF00; font-weight: normal;">sports</span>

to yeild the following results:

<a href="http://my_domain_name.com/sports/info.php"><img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" />Football <span style="color: #000000; background-color: #FFFF00; font-weight: normal;">sports</span> news</a>

Thanks in advance.

EDIT – Additional Information

Currently I am using the following 2-step method and it works for just the URLs, and not the title and alt tags. I also need to not replace the keywords in the title and alt tags too.

// Replaces both the website and general images path urls with character strings (used to prevent highlighting keywords found within the path urls)
   if(strpos('http://my_domain_name.com/sports', $keywords) != false) {
     $description = str_ireplace('http://my_domain_name.com/sports', '1q2w3e4r5t6y7u', $description);
   }
   if(strpos('http://my_domain_name.com/sports/images', $keywords) != false) {
     $description = str_ireplace('http://my_domain_name.com/sports/images', '7u6y5t4r3e2w1q', $description);
   }

// Highlights the Search Keywords
   $description = str_ireplace($keywords, '<span style="color: #000000; background-color: #FFFF00; font-weight: normal;">'.$keywords.'</span>', $description);

// Replaces the character strings with the website and general images path urls
   if(strpos('http://my_domain_name.com/sports', $keywords) != false) {
     $description = str_ireplace('1q2w3e4r5t6y7u', 'http://my_domain_name.com/sports', $description);
   }
   if(strpos('http://my_domain_name.com/sports/images', $keywords) != false) {
     $description = str_ireplace('7u6y5t4r3e2w1q', 'http://my_domain_name.com/sports/images', $description);
   }
  • 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-05T16:45:37+00:00Added an answer on June 5, 2026 at 4:45 pm

    Just working with strings I would just do the following since all the attribute values always come before the element value it’s easy to get the right match, then just use a callback to replace ‘sports’ with whatever you like.

    probably more what you need:

    function replacer($match)
    {
        global $replace_match_with_this, $string_to_replace;
        return str_ireplace($string_to_replace, $replace_match_with_this, $match[0]);
    }
    
    $new_string = preg_replace_callback(sprintf('/>[^<>]*[\s-]+%s[\s-]+[^<>]*<\/a>/i', $keyword), 'replacer', $string, 1);
    

    presumably $keyword and $string_to_replace hold the same value and can be combined into one variable.

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

Sidebar

Related Questions

Possible Duplicate: replace all occurrences in a string I found this question/answer: Use JavaScript
How can I find multiple occurrences of a string within a string? And replace
How can I replace all the with the raw string \ in a string
I have a string like AxxBCyyyDEFzzLMN and I want to replace all the occurrences
How can I replace all occurrences of double quotes and single quotes with a
I got a string in which I replace all occurrences of [CODE]...[/CODE] . With
How can I write a SQL query to replace all occurrences of space in
I know I can use RegEx to replace all occurrences of 'a', 'b', or
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
I would like to search and replace all occurrences outside of <>. I can

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.