preg_replace URL not working - where is it wrong tho?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

preg_replace URL not working - where is it wrong tho?

Post by simonmlewis »

Code: Select all

<?php
function curPageURL()
{
 $pageURL = (isset($_SERVER['HTTPS']) && ($_SERVER["HTTPS"] == "on")) ? 'https://' : 'http://';
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$url = curPageURL();
$original = "fred"; 
$replace = "Sally"; 
$domainreplace = preg_replace ('$original', '$replace', '$url');  
echo "$url<br/>$original<br/>$replace<br/>Go to: $domainreplace";
?>
I need to replace one word inthe url's domain, with another word.
I think it's going wrong because $url will have :// and other // in it. Maybe even & signs.
How do I escape all those, and make this work?

Right now it is not echoing "$domainreplace".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_replace URL not working - where is it wrong tho?

Post by Celauran »

First problem is that your variables in the preg_replace call are in single quotes, meaning they're being treated as string literals. Remove the quotes and try again.
Post Reply