Page 1 of 1

preg_replace URL not working - where is it wrong tho?

Posted: Mon Jun 16, 2014 7:15 am
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".

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

Posted: Mon Jun 16, 2014 8:22 am
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.