0

I am trying to use PHP to change all dynamic URLs inside a div to a different, static URL.

For example I have:

<div class="mydiv">
  <a href="http://oldsite.com/123">Title 123</a>
  <a href="http://oldsite.com/321">Title 321</a>
  <a href="http://oldsite.com/abc">Title abc</a>
  <a href="http://oldsite.com/cba">Title cba</a>
</div>

and I want to change them all to:

<div class="mydiv">
  <a href="http://newsite.com">Title 123</a>
  <a href="http://newsite.com">Title 321</a>
  <a href="http://newsite.com">Title abc</a>
  <a href="http://newsite.com">Title cba</a>
</div>

I understand that I could do this with htaccess, but would prefer to do it in PHP if its possible. Any ideas?

EDIT: The oldsite.com links were generated from an RSS feed and are being embedded onto the page. I want all the RSS title links to just send the user to the new site home page.

Kieran
  • 2,554
  • 3
  • 26
  • 38
JB.
  • 893
  • 3
  • 16
  • 29
  • 2
    I don't understand the question. – Niklas B. Jan 14 '12 at 19:51
  • `str_replace( 'http://oldsite.com/', 'http://newsite.com/', $content )` – JJJ Jan 14 '12 at 19:51
  • you could make a variable and change the variable when you need. $URL = 'http://anysite.com/'; and then echo 'Title'; Is that what you mean or I just totally misunderstood? – Claudio Jan 14 '12 at 19:53
  • I think he means doing URL rewrites with PHP, not actually replacing HTML code. – Kieran Jan 14 '12 at 19:58
  • it would help to see the code that creates the dynamic url's in the first place ... if it's php you're trying to manipulate give us a clue as to what you're working with instead of the final html. – Silvertiger Jan 14 '12 at 20:23
  • @NiklasBaumstark Sorry for the lack of info. Ive updated the description. The links are placed on the page from a rss feed and I just want to change the location of the links to bring the users to the new site home page instead of the old sites rss feed. – JB. Jan 14 '12 at 21:47
  • @Josh: Do you have the HTML content in a string variable? Or do you want to post-process the complete output before it is send to the user? Or something entirely different? Please post PHP code that you have tried. – Niklas B. Jan 14 '12 at 21:50
  • Use rewrite rules through htaccess files or httpd.conf. PHP is an HTML templating engine; don't make it do the job of your web server. – jmkeyes Jan 14 '12 at 21:51
  • @NiklasBaumstark ideally, the link is changed in the actual source so that the user gets the actual link to the new url before the page loads – JB. Jan 14 '12 at 21:59
  • @JoshuaK what would be the best htaccess that would make all variables of oldsite.com just point to the newsite.com home page? – JB. Jan 14 '12 at 22:00
  • @Josh: So post-processing? In that case you either want to capture all output with output buffering (`ob_start` and the alikes), then apply your transformations, then output. Or better, use PHP at the place where you create this HTML and put the right URL in there right from the beginning. – Niklas B. Jan 14 '12 at 22:01
  • @Josh: This is not a task that can be solved by `mod_rewrite`, at least not if I understand you correctly. – Niklas B. Jan 14 '12 at 22:01
  • RewriteRules wouldn't physically change the HTML, it would just change the response that the server gives to clients that request that URL. – jmkeyes Jan 14 '12 at 22:02
  • @JoshuaK yea, thats why I am trying to stick to PHP if I can – JB. Jan 14 '12 at 22:06
  • @NiklasBaumstark I was able to get it using your guidance. Thanks so much – JB. Jan 14 '12 at 22:21

3 Answers3

3

PHP can replace a string with another string in your HTML output, yes, but this doesn't mean it's a good idea. This will appear to achieve exactly what you want on the surface, but break in subtle ways and (in the case of a website crawled by a search engine spider) it will break existing clients that expect URLs to remain the same location.

Instead of trying to outsmart HTTP with PHP just use HTTP to your advantage. If you have access to the server of oldsite.com and all the links for it must be redirected to the new server at newsite.com at the same URL paths, then use your web server to inform clients with an HTTP/1.0 301 Permanently Moved response and a Location: http://newsite.com/foo-bar-baz header.

This will inform ALL HTTP clients of your website that the old content can be found at the new address, and also inform the client to update it's bookmarks and to invalidate it's cached location and other stored information. mod_rewrite makes this absolutely trivial in Apache but if you absolutely must use PHP then as a last resort use it's header() function to send the same HTTP headers that mod_rewrite would.

jmkeyes
  • 3,751
  • 17
  • 20
0
<?php str_replace( 'http://oldsite.com/', 'http://newsite.com/', $content );?>

str_replace takes 3 parameters. The first is what you want to be changed (needle), the second is what you want it changed to, and the third is what you are looking in (haystack).

You could also use an inline ternary operator:

<?php($link==$old_link)? 'http://newsite.com/': 'http://oldsite.com/';?>
toon81
  • 868
  • 1
  • 5
  • 13
crmepham
  • 4,676
  • 19
  • 80
  • 155
  • hm that doesnt quite seem to work. Ive updated the description to add more info if that helps – JB. Jan 14 '12 at 21:48
0

You can do that using regular expressions:

$content = preg_replace("|http://oldsite.com(/([^'"]*)?)?|msi", "http://newsite.com", $content);

This will change all URLs starting with http://oldsite.com to http://newsite.com.

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39