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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:23:36+00:00 2026-05-28T20:23:36+00:00

I have the following, simple code: $text = str_replace($f,'<a href=’.$f.’ target=_blank>’.$u.'</a>’,$text); where $f is

  • 0

I have the following, simple code:

$text = str_replace($f,'<a href="'.$f.'" target="_blank">'.$u.'</a>',$text);

where $f is a URL, like http://google.ca, and $u is the name of the URL (my function names it ‘Google’).

My problem is, is if I give my function a string like

http://google.ca http://google.ca

it returns

<a href="<a href="http://google.ca" target="_blank">Google</a>" target="_blank">Google</a> <a href="<a href="http://google.ca" target="_blank">Google</a>" target="_blank">Google</a>

Which obviously isn’t what I want. I want my function to echo out two separate, clickable links. But str_replace is replacing the first occurrence (it’s in a loop to loop through all the found URLs), and that first occurrence has already been replaced.

How can I tell str_replace to ignore that specific one, and move onto the next? The string given is user input, so I can’t just give it a static offset or anything with substr, which I have tried.

Thank you!

  • 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-28T20:23:36+00:00Added an answer on May 28, 2026 at 8:23 pm

    One way, though it’s a bit of a kludge: you can use a temporary marker that (hopefully) won’t appear in the string:

    $text = str_replace ($f, '<a href="XYZZYPLUGH" target="_blank">' . $u . '</a>',
                         $text);
    

    That way, the first substitution won’t be found again. Then at the end (after you’ve processed the entire line), simply change the markers back:

    $text = str_replace ('XYZZYPLUGH', $f, $text);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple DOM code like the following <div> <div> </div> </div> I
I have the following simple code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
I have a simple Web Form with code like this: //... //tons of text
I have uploaded the following simple HTML code to http://losthobbit.net/temp/anchor.html <!DOCTYPE HTML PUBLIC -//W3C//DTD
I have the following simple code, that reads contents of a text file into
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have got the following very simple code: function init() { var articleTabs =
I have the following code (correct for my simple tests) for a linked list
I have used following code to create a simple PDF file. It executes fine

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.