How to get only center domain name from url - php

I have many thousands of urls from which i only want to get name of domain for example
http://google.com
<?php
$url = 'http://google.com';
$host = parse_url($url);
echo '<pre>';
print_r($host['host']);
echo '</pre>';
**//Output google.com**
?>
but i only want to get google from http://google.com not google.com
please help thanks

Not particularaly elegant but something like this gets simply the domain name...
$url = 'http://dev.subdomain.google.com';
$host = parse_url($url,PHP_URL_HOST);
$pieces=explode( '.', $host );
$popped=array_pop( $pieces ); //remove tld extension from stack
if( strlen( $popped ) <= 3 ) array_pop( $pieces ); //tld was likely a multi-part ext like .co.uk so pop next element off stack too!
$domain=array_pop( $pieces );
echo $domain; // returns 'google'

$url = 'http://google.com';
$host = parse_url($url);
$host = strstr($host, '.com', true);
See php.net/strstr for more detailed information, of course there's other and properly better ways to do it.

Try below code
<?php
$full_url = parse_url('http://facebook.com');
$url = $full_url['host'];
$url_array = explode('.',$url);
echo $url_array[0];
?>

maybe you can fix it with a regex
$host = (preg_replace("#(http://)|(https://)|\.(com)|(co\.uk)|(fr)|(de)|(org)|(net)#", "", $host));
preg_replace : preg_replace manual (php.net)
test your regex : Debuggex

Related

Get middle part of url

I am having several urls, such as:
http://stackoverflow.com
https://google.com
facebook.com
cnn.com/
https://help.uber.com/
I would only like to get back for every url the middle part, such as:
facebook, cnn, uber, stackoverflow, google.
I tried the following, where $line is the url:
$parts = parse_url($line);
$path_parts = explode('/', $parts['path']);
echo $path_parts[count($path_parts)-1];
However, as a return I do not get anything echoed out. The urls are also correctly read in!
Any suggestions what I am doing wrong?
I appreciate your replies!
Try using host like this. if your giving url like cnn.com parse_url parse it and will store it in path index.
<?php
$line='https://help.uber.com/';
$parts = parse_url($line);
$path_parts = explode('.', isset($parts['host'])?$parts['host']:$parts['path']);
echo $path_parts[count($path_parts)-2];
?>
You have to get the details first and use preg_match to get the format . Check the code below and you will get your desired output .
<?php
$line = "stackoverflow.com" ;
$parts = parse_url($line);
$domain_name = isset($parts['host']) ? $parts['host'] : $parts['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain_name, $dom)){
$d_name = explode(".", $dom['domain']) ;
echo $d_name[0];
}
?>

PHP regex to exactly obtain a string I want

I have a code for embedding a link for iframe.
$post_contetn = explode('htt',$content);
$content_with_link = $post_contetn[0];
$link = 'htt'.$post_contetn[1];
But the problem is that, if I write
http://www.espn.com was great
then it links "was great" is part of the $link.
How can I change (perhaps use regex) to only include the actual url?
======
If I incorporate siam's answer, should it be
$regex = '/https?:\/\/.*?(?=\s)/';
$post_contetn = preg_match($regex, $content, $linkarray);
$content_with_link = $post_contetn[0];
$link = $linkarray[0]
echo $content_with_link;
I then edited to
preg_match($regex, $content, $post_contetn);
$content_with_link = $post_contetn[0];
$link = $post_contetn[0]
echo $content_with_link;
But the error still occurs at echo line.
Try using the following regex :
(?:https?:\/\/\S+)?\S+\.\S+\.?\S+
see demo / explanation
PHP
<?php
$content = 'http://www.espn.com was great';
$regex = '/(?:https?:\/\/\S+)?\S+\.\S+\.?\S+/';
preg_match($regex, $content, $post_contetn);
$link = $post_contetn[0];
echo $link;
?>

Getting A Certain Part Of A Url

How can i get a certain part of a URL i have searched google and read a few tutorials but can't seem to get my head around it maybe someone can show me from the example below.
Here is my code
<?php
include "simple_html_dom.php";
$title = "fast";
$html = file_get_html("http://www.imdb.com/find?q=".urlencode($title)."&s=all");
$element = $html->find('[class="result_text"] a', 1);
$link = $element->href;
echo $link;
// Clear dom object
$html->clear();
unset($html);
?>
Now this echos
/title/tt0109772/?ref_=fn_al_tt_2
But i only want the imdb id.
tt0109772
So can someone explain or show me how to do this please
thanks
If it's always the subdir:
echo basename(dirname($link));
you can Use explode():
$str = "/title/tt0109772/?ref_=fn_al_tt_2";
$arrUrl = explode("/",$str);
$id = $arrUrl[2];
echo $id;
demo
You can use explode like:
$parts = explode("/", $url);
This will split URL to array. Then check what index you want.. and get it like:
$id = $parts[3];
You can debug with: print_r($parts);
Not the most elegant way, but
$link = '/title/tt0109772/?ref_=fn_al_tt_2';
$imdb_id = explode('/', $link);
$imdb_id = $imdb_id[2];
echo $imdb_id;
Will work.
Try this code: (source)
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', $path);
Then I think you need $segments[1] to get the IMDB id.

Error using preg_relace to change url youtube?

I have a sample code:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$replace = $pattern.'&w=550';
$string = preg_replace($pattern, $replace, $url);
?>
How to result is http://www.youtube.com/watch?v=KTRPVo0d90w&w=550
You can just append using the . operator:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$string = $url.'&w=550';
?>
Use preg_match instead:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w&s=222';
$pattern = '/v=[^&]+/i';
preg_match($pattern, $url, $match);
echo 'http://www.youtube.com/watch?'.$match[0].'&w=550';
?>
Like below?
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$bit = '&w=550';
echo "${url}${bit}";
Don't get me wrong, I'm not looking to gain any points here, but just thought I would add to this question and include a few options. I love toying with ideas like this every once in a while.
Using jh314's idea to concatenate the strings, thought that this could be used for future use, to actually replace a string inside the video's YouTube number, should the occasion ever present itself.
Such as $number for instance.
<?php
$url = 'http://www.youtube.com/watch?v=';
$number = 'KTRPVo0d90w';
$string = $url.$number.'&w=550';
// Output to screen
echo $string;
echo "<br>";
// Link to video
echo "Click for the video";
?>
The same could easily be done for the video's width.

Strip URL only display domain

I have a url, http://members.exampledomain.com, and I would like to display only exampledomain onto my page. strip out 'http://members' and '.com'
For example http://members.exampledomain.com's index page has something like
<img src="members/images/logo<?=$exampledomain; ?>.png" />
ok this worked:
<?php
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$ArrayOfStrings = explode(".", $url);
echo $ArrayOfStrings[1];
include("variables/" . $ArrayOfStrings[1] . "/config.inc");
?>
You included an explode tag. I'm assuming you are looking for help using the explode function?
<?php
$url = "http://memebers.exampledomain.com";
$ArrayOfStrings = explode(".", $url);
echo $ArrayOfStrings[1];
?>
I annotated this code to make it readable. Do some research next time:
<?php
$url = "http://members.exampledomain.com/";
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$host_exploded = explode(".", $host);
echo $host_exploded[count($host_exploded) - 2];
?>

Categories