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







funkimunkyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.