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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:47:11+00:00 2026-05-15T15:47:11+00:00

I need to replace a word with another word. But $var= str_replace($linklabel[$k], $linklabelmod[$k], $var);

  • 0

I need to replace a word with another word. But

$var= str_replace($linklabel[$k], $linklabelmod[$k], $var);

is not giving the desired result. For example i have a string

$var="the theory of them thesis"; 
$linklabel[1]="the"; 
$linklabelmod[1]="hhh";

What i need is, i just want to replace the word “the”. But since “the” is repeated in “theory” “thesis” and “them”, all those starting three letters are also getting replaced. Then output becomes
$var=”hhh hhhory of hhhm hhhsis”;//wrong
But i need the output
$var=”hhh theory of them thesis”;//write
I am bad at explaining a question, plz excuse me…

Thanks in advance….


From what I (paxdiablo) can gather from the OP’s comments, this is the code following modifications (still claimed not to work):

foreach($xpath->query('//a') as $element) {
    $linklabel[] = $element->textContent;
    $link[] = $element->getAttribute("href");
    $i=$i+1;
}
for($k=0;$k<$i;$k++) {
    $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]);
    $var = preg_replace ('/\b'.preg_quote($linklabel[$k]).'\b/', $linklabelmod[$k], $var);
}
print $var; //printing web page
  • 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-15T15:47:12+00:00Added an answer on May 15, 2026 at 3:47 pm

    Insert white space 🙂 if you want to use str_replace function

    php > $var = "the theory of them thesis";

    php > $var = str_replace(array(" the ", "the "), 'hhh', $var);

    php > echo $var;

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

Sidebar

Related Questions

I have the following: param=/var/tmp/test I need to replace the word test with another
I want create Word document from another Word document, but I need replace some
I need to replace a word that starts with % . For example Welcome
I have a word : [lesserthen] , that I need to replace with <
I'm writing an application in PHP, and I need to replace any word between
I have a string that I would like to replace with another one if
I have a html source as a String variable. And a word as another
Well, I need to replace a word, in a div contentEdible property on, by
I have an function in ASP VB. and I need to replace the exact
File1: hello world foo bar a word with a space I need to replace

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.