prakash Posted December 13, 2007 Share Posted December 13, 2007 suppose $var="http://www.website.com"; $var="http://website.com"; $var="www.website.com"; $var="http://www.www.com"; $var="http://www.com"; so how can I stripe http:// or http://www or www of the above var using php output must be website.com www.com Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/ Share on other sites More sharing options...
phpQuestioner Posted December 13, 2007 Share Posted December 13, 2007 <?php $var="http://www.google.com"; $myvar = explode(".", $var); echo $myvar[0]; echo $myvar[3]; ?> you will need an array for multiple values in one variable. Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414058 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 <?php $var="http://www.google.com"; $myvar = explode("//", $var); echo $myvar[1]; ?> you will need an array for multiple values in one variable. this will produce www.google.com or www.www.com but I need to produce only google.com and www.com any idea Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414062 Share on other sites More sharing options...
phpQuestioner Posted December 13, 2007 Share Posted December 13, 2007 try my code again - I had to correct it Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414064 Share on other sites More sharing options...
yzerman Posted December 13, 2007 Share Posted December 13, 2007 parse_url($var, PHP_URL_HOST); Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414070 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 Here's a solution that uses the parse_url() function: <?php $vars = array(); $vars[]="http://www.website.com"; $vars[]="http://website.com"; $vars[]="www.website.com"; $vars[]="http://www.www.com"; $vars[]="http://www.com"; foreach($vars as $url) { echo 'Original url: ' . $url . "<br>\n"; //debug if (strtolower(substr($url,0,7)) != 'http://') $url = 'http://' . $url; $tmp = parse_url($url); $tmp2 = explode('.',$tmp['host']); if (count($tmp2) > 2) $dmp = array_shift($tmp2); echo implode('.',$tmp2)."<br>\n"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414071 Share on other sites More sharing options...
phpQuestioner Posted December 13, 2007 Share Posted December 13, 2007 Actually, I think you will need to use explode_assoc(). Update: Skip the above statement I just made - do this: <?php $var="http://www.google.com"; $myvars = explode(".", trim($var, "http://")); echo $myvars[0]; echo "."; echo $myvars[2]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414072 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 I finally made my own $var="http://www.site.com"; $var="http://www.subdomain.site.com"; $var="http://www.www.site.com"; $var="http://www.www.com"; $var="www.com"; $var="http://www.phpfreaks.com/forums/index.php?action=post;topic=171945.0;num_replies=6"; etc.... <?php $query=parse_url(htmlspecialchars(trim($var)), PHP_URL_HOST); $myvar= explode(".", $query); $totalIndex=count($myvar); $ext=$myvar[$totalIndex-1]; $domainName=$myvar[$totalIndex-2]; $query=$domainName.'.'.$ext; ?> just tested and works gr8 for every urls Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414077 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 UPDATE: not working for urls like www.com or site.com any help? Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414089 Share on other sites More sharing options...
phpQuestioner Posted December 13, 2007 Share Posted December 13, 2007 try this: <?php $var="http://www.site.com"; $myvars = explode(".", trim($var, "http://")); $query="". $myvars[0] .".". $myvars[2] .""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414100 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 Use the code I posted. It works for all of the examples you have shown so far. Ken Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414106 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 Use the code I posted. It works for all of the examples you have shown so far. Ken that code doesn't work for http://www.subdomain.site.com or http://www.www.site.com, Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414119 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 Yes it does. Here are the results I get for those two examples: Original url: http://www.subdomain.site.com subdomain.site.com Original url: http://www.www.site.com www.site.com Original url: www.subdomain.site.com subdomain.site.com Original url: www.www.site.com www.site.com Ken Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414126 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 Yes it does. Here are the results I get for those two examples: Original url: http://www.subdomain.site.com subdomain.site.com Original url: http://www.www.site.com www.site.com Original url: www.subdomain.site.com subdomain.site.com Original url: www.www.site.com www.site.com Ken yes but I said only name plus extension not the www or subdomain prefix Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414127 Share on other sites More sharing options...
phpQuestioner Posted December 13, 2007 Share Posted December 13, 2007 is this all you want; "subdomain.website.com" from the original "http://www.subdomain.website.com"? if it is - just do this: <?php $var="http://www.subdomain.website.com"; $myvar = trim($var, "http://www."); echo "$myvar"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414128 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 When you have something like http://www.www.example.com/ only the first "www." is the prefix, the rest is the domain (or subdomain). Since now you're saying you only want the true domain part replace this line in my code <?php if (count($tmp2) > 2) $dmp = array_shift($tmp2); ?> with <?php while (count($tmp2) > 2) $dmy = array_shift($tmp2); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414131 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 When you have something like http://www.www.example.com/ only the first "www." is the prefix, the rest is the domain (or subdomain). Since now you're saying you only want the true domain part replace this line in my code <?php if (count($tmp2) > 2) $dmp = array_shift($tmp2); ?> with <?php while (count($tmp2) > 2) $dmy = array_shift($tmp2); ?> Ken now seems it's 100% perfect. works on all example urls. thanks ken for you help Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414140 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 in addition I also want to add ip address on $var so how can it be added when I pass $var="http://111.11.11.11/"; or $var="111.11.11.11"; it's not supported and outputs only 11.11 any help? Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414167 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 Here is my code, reworked to satisfy this new condition: <?php $vars = array(); $vars[]="http://www.website.com"; $vars[]="http://website.com"; $vars[]="www.website.com"; $vars[]="http://www.www.com"; $vars[]="http://www.com"; $vars[]="http://www.phpfreaks.com/forums/index.php?action=post;topic=171945.0;num_replies=6"; $vars[]='http://site.com/'; $vars[]='site.com'; $vars[]='http://www.subdomain.site.com'; $vars[]='http://www.www.site.com'; $vars[]='www.subdomain.site.com'; $vars[]='www.www.site.com'; $vars[]="http://111.11.11.11/"; $vars[]="111.11.11.11/"; $vars[]='this.is.12.test.com'; foreach($vars as $url) { echo 'Original url: ' . $url . "\n"; if (strtolower(substr($url,0,7)) != 'http://') $url = 'http://' . $url; $tmp = parse_url($url); $tmp2 = explode('.',$tmp['host']); if (count($tmp2) != 4 || !is_ip_addr($tmp2)) while (count($tmp2) > 2) $dmy = array_shift($tmp2); echo implode('.',$tmp2)."\n"; } function is_ip_addr($ary) { $ok = true; foreach($ary as $part) $ok = is_numeric($part); return ($ok); } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414177 Share on other sites More sharing options...
prakash Posted December 13, 2007 Author Share Posted December 13, 2007 Here is my code, reworked to satisfy this new condition: <?php $vars = array(); $vars[]="http://www.website.com"; $vars[]="http://website.com"; $vars[]="www.website.com"; $vars[]="http://www.www.com"; $vars[]="http://www.com"; $vars[]="http://www.phpfreaks.com/forums/index.php?action=post;topic=171945.0;num_replies=6"; $vars[]='http://site.com/'; $vars[]='site.com'; $vars[]='http://www.subdomain.site.com'; $vars[]='http://www.www.site.com'; $vars[]='www.subdomain.site.com'; $vars[]='www.www.site.com'; $vars[]="http://111.11.11.11/"; $vars[]="111.11.11.11/"; $vars[]='this.is.12.test.com'; foreach($vars as $url) { echo 'Original url: ' . $url . "\n"; if (strtolower(substr($url,0,7)) != 'http://') $url = 'http://' . $url; $tmp = parse_url($url); $tmp2 = explode('.',$tmp['host']); if (count($tmp2) != 4 || !is_ip_addr($tmp2)) while (count($tmp2) > 2) $dmy = array_shift($tmp2); echo implode('.',$tmp2)."\n"; } function is_ip_addr($ary) { $ok = true; foreach($ary as $part) $ok = is_numeric($part); return ($ok); } ?> Ken well done ken you are really a far better than genius. this is the gr8 logical help I have ever got from phpfreaks phpfreaks rocks Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414182 Share on other sites More sharing options...
prakash Posted December 14, 2007 Author Share Posted December 14, 2007 hi, got errors again this is not working for domains like http://www.site.co.uk how to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-414871 Share on other sites More sharing options...
prakash Posted December 18, 2007 Author Share Posted December 18, 2007 is there anyone to fix it Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-417753 Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Have another look at parse_url. Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-417762 Share on other sites More sharing options...
sasa Posted December 18, 2007 Share Posted December 18, 2007 try <?php $var="http://www.com/"; //$var="http://www.phpfreaks.com/forums/index.php?action=post;topic=171945.0;num_replies=6"; preg_match('/([^\.\/]+\.[^\.\/]+)($|\/)/',$var,$out); echo $out[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/81545-striping-some-thing-from-a-variable/#findComment-417881 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.