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! 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(). 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. 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. 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... ?> 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. 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 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. 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 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. 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 "/" 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); 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! Link to comment https://forums.phpfreaks.com/topic/128123-solved-manipulating-a-string/#findComment-663546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.