• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

how to user preg_replace for changing urls

I am getting a html page from another server which has a bunch of links e.g.
<li><a href="/pages/departments/english/index.php">English</a></li>
        <li><a href="/pages/departments/maths/index.php">Maths</a></li>
        <li><a href="/pages/departments/home_ec/index.php">Home Ec.</a></li>
        <li><a href="/pages/departments/history/index.php">History</a></li>
       
I want to replace all these links so
<a href="/pages/departments/maths/index.php">
becomes
<a href="http://newserver.local/pages/departments/maths/index.php">

my code is

<?php
$homepage = file_get_contents('http://server.local/menu/sendmenu.php');

$pattern = 'href="/';
$replacement = 'href="server.local/';
$newcontent = preg_replace($pattern, $replacement, $homepage);

echo $newcontent;
?>

when I debug this in eclipse i get the errors below and $newcontent is just null

Debug Warning: /wordpressmu/html/getmenu.php line 6 - preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Delimiter must not be alphanumeric or backslash

Debug Strict (PHP 5): /wordpressmu/html/getmenu.php line 6 - preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '1.0/DST' instead







0
funkimunky
Asked:
funkimunky
  • 2
1 Solution
 
theodorejsalvoCommented:
Why not use str_replace instead of preg_replace?
$newcontent = str_replace($pattern, $replacement, $homepage);

Open in new window

0
 
Terry WoodsIT GuruCommented:
You just missed the delimiters around the pattern. I've used # characters.

I also added the i pattern modifier, in case some links have HREF in uppercase.
<?php
$homepage = file_get_contents('http://server.local/menu/sendmenu.php');

$pattern = '#href="/#i';
$replacement = 'href="server.local/';
$newcontent = preg_replace($pattern, $replacement, $homepage);

echo $newcontent;
?>

Open in new window

0
 
Terry WoodsIT GuruCommented:
Oh, it might also pay to allow space characters around the equals sign, and allow for it to span multiple lines too:

$pattern = '#href\s*=\s*"/#is';
0
 
funkimunkyAuthor Commented:
Thats fixed it thank you for your help. Especially pointing out improvements.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now