unknown1 Posted October 3, 2009 Share Posted October 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/ Share on other sites More sharing options...
Philip Posted October 3, 2009 Share Posted October 3, 2009 Use str_replace for something that doesn't need regex. <?php $url=$rs_nw['Url']; $remove = "http://"; $replace = ""; $url = preg_replace($remove, $replace, $url); echo $url; ?> Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/#findComment-929725 Share on other sites More sharing options...
cags Posted October 3, 2009 Share Posted October 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/#findComment-929733 Share on other sites More sharing options...
unknown1 Posted October 3, 2009 Author Share Posted October 3, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/#findComment-929738 Share on other sites More sharing options...
Philip Posted October 3, 2009 Share Posted October 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/#findComment-929743 Share on other sites More sharing options...
unknown1 Posted October 3, 2009 Author Share Posted October 3, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/#findComment-929752 Share on other sites More sharing options...
cags Posted October 3, 2009 Share Posted October 3, 2009 $site_url=str_replace("http://", "", $url); Quote Link to comment https://forums.phpfreaks.com/topic/176394-solved-remove-http-from-a-dynamic-url-with-php/#findComment-929758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.