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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:58:28+00:00 2026-05-26T08:58:28+00:00

How to wrap html tags for each search words, such as highlight words. I

  • 0

How to wrap html tags for each search words, such as highlight words. I tried my code here, but nothing changed. Where is the problem, or is there have any other good way? thanks.

<?php
$sentence = "for loops are the most complex loops in PHP. They behave like their C counterparts";//original
$search = 'php counterparts';//search words
$str = explode(' ',$search);//explode every search word
for($i=0;$i<10;$i++){
    if($str[$i]!=''){ //make sure if $str[$i] is not empty, do $newstr.
        $newstr .= '\'<b>'.$str[$i].'</b>\','; //wrap <b> tag for search words
    }
}
$newstr = substr($newstr, 0, strlen($newstr)-1);//remove last common, combine a array

$new = str_ireplace(array($str),array($newstr),$sentence);
echo $new;
?>
  • 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-26T08:58:29+00:00Added an answer on May 26, 2026 at 8:58 am

    Why is the array bounded at 10 iterations? Why do you use a string to hold the modified words? Why the regex demarcations when your not using a regex function for the substitution?

    Don’t even try to fix this code – start again from scratch:

    $sentence = "for loops are the most complex loops in PHP. They behave like their C counterparts";//original
    $search = 'php counterparts';//search words
    $srch_words = explode(' ',$search);//explode every search word
    $replace_words=array();
    foreach ($srch_words as $key=>$val) {
         $replace_words[$key]='<b>' . $val . '</b>';
    }
    $sentence=str_ireplace($srch_words, $replace_words, $sentence);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to wrap some words with HTML tags, for that I am
I want to use link_to wrap some HTML,but I meet some problem. <%= link_to
I would like to highlight search terms on a page, but not mess with
I have an HTML string containing multiple <image> tags. I'd like to search for
In the past people used to wrap HTML comment tags around blocks of JavaScript
It's possible in plain HTML to wrap pieces of text in spans or tags
pre tags are super-useful for code blocks in HTML and for debugging output while
How would I go about extracting text between 2 html tags using delphi? Here
The pre tags in IE 7 do wrap the text but the table width
Is there a way to get an <input /> -field in HTML to wrap

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.