PDA

View Full Version : search string


esthera
06-04-2006, 10:32 AM
how can I take in a string and

if the string has 2 or more periods (anywhere within it to only take that after the second period(looking from teh right)?

and if the string has only one period then I look at the whole string?

lavinpj1
06-04-2006, 11:06 AM
Something like...

<?php
$string = "The.catsaton.themat";
$expl = explode(".", $string);
if (count($expl) > 2) {
unset($expl[0]);
unset($expl[1]);
$impl = implode($expl);
}
else {
$impl = $string;
}
echo $impl; //returns thematt
?>

~Phil~

esthera
06-04-2006, 12:13 PM
thanks

esthera
06-04-2006, 12:43 PM
no this is not what I need

because if I enter in xxx.google.com -- it should return google.com - now it returns the .com (I want it to return everything after the second . from the right)

and if no second . then return all

chump2877
06-04-2006, 02:46 PM
didn;t test this, but see if this works:

$URL = "www.yourSite.com";
preg_match_all("/\./", $URL, $matches);

if (count($matches[0]) == 1)
{
echo $URL;
}
elseif (count($matches[0]) >= 2)
{
$URL_array = explode(".", $URL);
unset($URL_array[0]);
$URL = implode(".", $URL_array);
echo $URL;
}
else
{
echo "The URL is no good.";
}

Edit: Looks like you can also use substr_count() (instead of preg_match_all()) to count occurences of a period in your string: http://us3.php.net/manual/en/function.substr-count.php

esthera
06-04-2006, 03:07 PM
so in yoru case how would I know everythign after the second .
meaning

www.yahoo.com should return yahoo.com

http://yahoo.com shoudl return yahoo.com
and http://www.yahoo.com should return yahoo.com

and yahoo.com/mail/ should return yahoo.com --

how can I do this? I don't see how the above does it .

chump2877
06-04-2006, 03:13 PM
so in yoru case how would I know everythign after the second .
meaning

www.yahoo.com should return yahoo.com

http://yahoo.com shoudl return yahoo.com
and http://www.yahoo.com should return yahoo.com

and yahoo.com/mail/ should return yahoo.com --

how can I do this? I don't see how the above does it .


you're right it doesn;t do that because you didn;t ask for that ;)

chump2877
06-04-2006, 03:32 PM
here ya go try this, but next time try to be clearer about what you want :eek:

$URL = "www.yourSite.com";

$pos = strpos($URL, "http://");
if ($pos == 0)
{
$URL = substr($URL, 7);
}

$special_char_array = array("\", "/", "?", "#");
foreach ($special_char_array as $val)
{
if (strpos($URL, $val) !== false)
{
$URL_array2 = explode($val, $URL);
$URL = $URL_array2[0];
}
}

preg_match_all("/\./", $URL, $matches);

if (count($matches[0]) == 1)
{
echo $URL;
}
elseif (count($matches[0]) >= 2)
{
$URL_array = explode(".", $URL);
unset($URL_array[0]);
$URL = implode(".", $URL_array);
echo $URL;
}
else
{
echo "The URL is no good.";
}

esthera
06-04-2006, 04:35 PM
thanks so much for your help. Your right that I was not clear..

but I copied your code above exactly to test it out and I just get a blank screen when opening the page in my browser -- any ideas why?

chump2877
06-04-2006, 05:18 PM
the code was reacting strangely to the special character in the array...there might be a better way to do this, but i did test the following code and it appears to work:

<?

$URL = "http://www.yourSite.com/whatever/file.php#section1?name=name";

$pos = strpos($URL, "http://");
if ($pos === 0)
{
$URL = substr($URL, 7);
}


$special_pattern_array = array("/\?/", "/#/", "/\//");
foreach ($special_pattern_array as $val)
{
preg_match($val, $URL, $matches);
if (!empty($matches[0]))
{
$URL_array2 = preg_split($val, $URL);
$URL = $URL_array2[0];
}
}

preg_match_all("/\./", $URL, $matches);

if (count($matches[0]) == 1)
{
echo $URL;
}
elseif (count($matches[0]) >= 2)
{
$URL_array = explode(".", $URL);
unset($URL_array[0]);
$URL = implode(".", $URL_array);
echo $URL;
}
else
{
echo "The URL is no good.";
}

?>

Edit: It also just occurred to me that if all you want to do is chop off the "www." of your URLs, you can also use the following, shorter code:

<?

$URL = "http://www.yourSite.com/whatever/file.php#section1?name=name";

$pos = strpos($URL, "http://");
if ($pos === 0)
{
$URL = substr($URL, 7);
}

$special_pattern_array = array("/\?/", "/#/", "/\//");
foreach ($special_pattern_array as $val)
{
preg_match($val, $URL, $matches);
if (!empty($matches[0]))
{
$URL_array2 = preg_split($val, $URL);
$URL = $URL_array2[0];
}
}

$pos = strpos($URL, "www.");
if ($pos === 0)
{
$URL = substr($URL, 4);
}

?>

This method would also be better for URLs like http://yourSite.co.uk, for example....with the previous code, that URL would turn into co.uk, and I don;t think you want that, so a test for "www." is better to accomodate more kinds of URLs...

harsh789
06-05-2006, 04:36 PM
$parts = parse_url($url);
$domain = $parts['host'];
if (substr_count($domain, ".")>1)
$domain = substr(strstr($domain, "."), 1 );

chump2877
06-05-2006, 07:23 PM
$parts = parse_url($url);
$domain = $parts['host'];
if (substr_count($domain, ".")>1)
$domain = substr(strstr($domain, "."), 1 );

Doh, why didn;t I think to use prse_url()...that's a much quicker way to do the same thing, duh...:rolleyes:

I still think you'll want to test for "www." instead though...

$URL = "http://www.yourSite.com/whatever/file.php#section1?name=name";

$parts = parse_url($URL);
$hostname= $parts[host];

$pos = strpos($hostname, "www.");
if ($pos === 0)
{
$hostname = substr($hostname, 4);
}

echo $hostname;