Jump to content

striping some thing from a variable


prakash

Recommended Posts


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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

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.