Jump to content

[SOLVED] Remove http:// from a dynamic URL with php


unknown1

Recommended Posts

I wanted to remove the http:// from URL's coming from my database.

I was hoping someone can explain how I can do this...

 

Below is my attempt at do it but I get an error:

 

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash.

 

<a href="whoisView.php?domain=<?php   
	  
	  $url="$rs_nw[url]";
	  $remove = "http\:\/\/";
	   $replace = "";
	   preg_replace($remove, $replace, $url); 

	 echo "url"; ?>

 

 

Thanks in advance.

Incidently the reason you get the error message is the first character in your pattern parameter (in your case $remove) is the letter h. When using Regex with the preg_ functions, all strings must have delimiters at the start and end to signify the start and end of the pattern. Those characters cannot be alphanumeric or backslash. So for example you could have used

 

"#http://#"
"~http://~"
// and so on 

 

As KingPhilip suggested though, you should use str_replace.

This seems to work fine... thanks guys.

 

$remove = array("h","t","t","p",":","/","/");
	   $replace = array("","","","","","","");
	   $url= $rs_nw['Url'];
	   
	   $site_url=str_replace($remove, $replace, $url);

 

 

 

Uh you must not have a very broad amount of values in your testing range...

 

Test url: http://www.testingphilsplace.com/test

Output: www.esingilslace.comes

 

Why? Because when you use an array in str_replace like that, it will replace every value... so every h, t, p, : and / in the entire string would be replaced with nothing.

This seems to work fine... thanks guys.

 

$remove = array("h","t","t","p",":","/","/");
	   $replace = array("","","","","","","");
	   $url= $rs_nw['Url'];
	   
	   $site_url=str_replace($remove, $replace, $url);

 

 

 

Uh you must not have a very broad amount of values in your testing range...

 

Test url: http://www.testingphilsplace.com/test

Output: www.esingilslace.comes

 

Why? Because when you use an array in str_replace like that, it will replace every value... so every h, t, p, : and / in the entire string would be replaced with nothing.

 

lol, just noticed that too...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.