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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:05:03+00:00 2026-05-31T02:05:03+00:00

I try to replace href by condition: http://www.sitea.com/vip: => http://localhost/aaa?search= http://www.sitea.com/ => http://localhost/bbb/ (without

  • 0

I try to replace href by condition:

http://www.sitea.com/vip:  => http://localhost/aaa?search=
http://www.sitea.com/      => http://localhost/bbb/ (without 'vip:')
http://www.siteb.com/      => http://localhost/ccc/

I use strpos for judge:

$str='<a href="http://www.sitea.com/vip:vp_14098">link A</a><a href="http://www.sitea.com/contact">link B</a><a href="http://www.siteb.com/player">link C</a>';
if(strpos($str,'www.sitea.com')!== false){
    if(strpos($str,'vip:')!== false){
        $str = str_replace('http://www.sitea.com/', 'http://localhost/aaa?search=',$str);
    }else{
        $str = str_replace('http://www.sitea.com/', 'http://localhost/bbb/',$str);
    }
}
if(strpos($str,'www.siteb.com')!== false){
    $str = str_replace('http://www.siteb.com/', 'http://localhost/ccc/',$str);
}
echo $str;

But this output:

<a href="http://localhost/aaa?search=vip:vp_14098">link A</a>
<a href="http://localhost/aaa?search=contact">link B</a>
<a href="http://localhost/ccc/player">link C</a>

The second link not replace as I wish.

  • 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-31T02:05:04+00:00Added an answer on May 31, 2026 at 2:05 am

    Oh boy. You’re decisions are based on the results of strpos applied to a string containing all the links. Because $str contains a link of the first type, the corresponding if-condition is always true. You can use a regular expression to address that problem, or if this isn’t just a simplified example but your real world code, try this:

    $str='<a href="http://www.sitea.com/vip:vp_14098">link A</a><a href="http://www.sitea.com/contact">link B</a><a href="http://www.siteb.com/player">link C</a>';
    $str = str_replace('http://www.sitea.com/vip:', 'http://localhost/aaa?search=vip:',$str);
    $str = str_replace('http://www.sitea.com/', 'http://localhost/bbb/',$str);
    $str = str_replace('http://www.siteb.com/', 'http://localhost/ccc/',$str);
    echo $str;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use preg_replace to replace &lt;a href=&quot;WWW.ANYURL.COM&quot;&gt;DISPLAY_TEXT&lt;/a&gt; with <a href=WWW.ANYURL.COM>DISPLAY_TEXT</a> here is
I have the following url: $str = http://www.domain.com/data/images\flags/en.gif; I'm using str_replace to try and
When I try this: item.css(a).each do |a| if !a.starts_with? 'http://' a.replace a.content end end
When a user enters a URL, e.g. http://www.google.com , I would like to be
I try to replace a sub-str by the content of a valiable where its
I try to replace few characters in some string 14/04/2010 17:12:11 and get, for
I am using this tutorial to try to replace the default TitleBar with a
I am trying to replace accented characters with regular characters. When I try to
I try to replace a jquery chaned function with a var but it doesnt
I have some content from server using $.ajax() function. I try to replace some

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.