Lamez Posted October 12, 2008 Share Posted October 12, 2008 I am have strings in URL formats, like www.site.com/path how can I take off the /path part, and just have www.site.com? -Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/ Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 parse_url(). Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663523 Share on other sites More sharing options...
Lamez Posted October 12, 2008 Author Share Posted October 12, 2008 lol I am using that, but sometimes the url comes out like site.com/path, and when I pull the path out of the array, I get site.com/path. that is why I wanna make a custom function to take off the /path. Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663525 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 Just use the ['host'] part of the array that's returned. Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663528 Share on other sites More sharing options...
Lamez Posted October 12, 2008 Author Share Posted October 12, 2008 I have tried that, I get nothing. <?php //begin checking for blacklisted... $n_url = $_SESSION['n_url']; $par = parse_url($n_url); //print_r($par); echo $par['path']; $b_url = $par['host'];//grab base url if($b_url = ""){ $b_url = $par['path']; } echo "Input URL: ".$n_url; echo "<br>"; echo "Base: ".$b_url; echo "<br>"; $www = addwww($b_url); echo "Add WWW: ".$www; echo "<br>"; $pro = addPro($b_url); echo "Pro: ".$pro; echo "<br>"; $both = addwww($b_url); $both = addPro($both); echo "Both: ".$both; echo "<br>"; //end check, everything cleared... ?> Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663530 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 Because you assign $b_url to "" in your messed up if statement. Try: if ($b_url == "") { ... } Two = signs. Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663532 Share on other sites More sharing options...
Lamez Posted October 12, 2008 Author Share Posted October 12, 2008 well that was a mistake, but I still get site.com/path Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663534 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 Give me an example URL that you're trying to parse. Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663535 Share on other sites More sharing options...
Lamez Posted October 12, 2008 Author Share Posted October 12, 2008 youtube.com/watch?v=h0JX5jWv-tk example: http://links.krazypicks.com/?url=youtube.com/watch?v=h0JX5jWv-tk Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663536 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 <?php $url = 'http://www.youtube.com/watch?v=h0JX5jWv-tk'; print_r(parse_url($url)); ?> OUTPUT: Array ( [scheme] => http [host] => www.youtube.com [path] => /watch [query] => v=h0JX5jWv-tk ) ['host'] is clearly correct. Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663538 Share on other sites More sharing options...
Lamez Posted October 12, 2008 Author Share Posted October 12, 2008 ugh. I do not have the protocol added. If I add the protocol, then it works like a charm. Otherwise I need to get rid of everything after the slash "/" Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663540 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 $str = preg_replace('~/(.+)$~', '', $str); Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663541 Share on other sites More sharing options...
Lamez Posted October 12, 2008 Author Share Posted October 12, 2008 Thank you! That worked great! Quote Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663546 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.