Extract url from this string? It should be flexible for all
strings, not for this string only.
"yahoo.comyahoo.co.inhotmail.org"



Extract url from this string? It should be flexible for all strings, not for this string only. &q..

Answer / amitverma

##
##This will work for PHP 5.3 -

$url1="yahoo.com";
$url2="yahoo.co.in";
$url3="hotmail.org";

$domain1 = strstr($url1, '.', true);
$domain2 = strstr($url2, '.', true);
$domain3 = strstr($url3, '.', true);

echo $domain1, $domain2, $domain3;


##
## For Older PHP (<5.2.8), this code will follow -
$url1="yahoo.com";
$url2="yahoo.co.in";
$url3="hotmail.org";

$domain1 = substr($url1, 0, strpos($url1, '.') );
$domain2 = substr($url2, 0, strpos($url2, '.') );
$domain3 = substr($url3, 0, strpos($url3, '.') );

echo $domain1, $domain2, $domain3;


Is This Answer Correct ?    2 Yes 8 No

Post New Answer

More PHP Interview Questions

What is the use of isset() in php?

0 Answers  


What is mysql_fetch_array?

0 Answers  


How to create a table using php?

0 Answers  


Does jwt protect against csrf?

0 Answers  


How can we extract string 'techinterviews.com' from a string 'http://www.techinterviews.com' using regular expression in PHP?

4 Answers  






Do you know what does $globals means?

0 Answers  


Explain some of the php string functions?

0 Answers  


Who developed php?

0 Answers  


Tips to optimize the php script..... Suggestion for exception handling in php...

1 Answers  


How to create an empty array in php?

0 Answers  


How can I check if a value is already in an array?

3 Answers   Rushmore Consultancy,


What does namespace mean in php?

0 Answers  


Categories