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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.