Get middle part of url - php

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];
}
?>

Related

How to get only center domain name from url

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

Stripping url with preg_replace

I needed to strip the
http://www.
from a domain name and also anything following it such as
/example
so that i would just be left with yourdomain.com
I added the following code to a file:
$domain = HTTP_SERVER;
$domain_name = preg_replace('/^https?:\/\/(?:www\.)?/i', '', $domain);
But if i echo $domain_name I still get a url such as yourdomain.com/testsite
Can anyone see what i have done wrong here as it has not removed the /testsite and i thought i had got this right.
use this
$url = 'http://www.example.co.uk/directory/level1/last/page.html';
$parse= parse_url($url);
preg_match ("/\.([^\/]+)/", $parse['host'], $mydomain);
echo $mydomain[1];
This may be a hack that someone will disagree with, but i resolved the problem by using the following code.
$url = HTTP_SERVER;
$parse = parse_url($url);
$domain = $parse['host'];
$domain_name = preg_replace('/(?:www\.)?/i', '', $domain);
echo $domain_name;
If you can see a reason why this should not be used, please feel free to let me know. Always something new to learn :)

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.

URL Replacement in PHP

I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;

How can I know if it's a absolute domain name with PHP

I am getting a link and in it there is a href... I want to know if it's a
http://[...].com/file.txt absolute domain name
or
/file.txt
a link that does not have the full URL.
How can I do this with PHP?
Use parse_url and see if you get a scheme and host. For example, this:
$url = 'http://username:password#hostname/path?arg=value#anchor';
$parts = parse_url($url);
echo $url, "\n", $parts['scheme'], "\n", $parts['host'], "\n\n";
$url = '/path?arg=value#anchor';
$parts = parse_url($url);
echo $url, "\n", $parts['scheme'], "\n", $parts['host'], "\n\n";
Produces:
http://username:password#hostname/path?arg=value#anchor
http
hostname
/path?arg=value#anchor
Live example: http://ideone.com/S9WR2
This also allows you to check the scheme to see if it is something you want (e.g. you'd often want to ignore mailto: URLs).
You can use preg_match with a regexp to test the format of the url string. You'll need to modify $myurl as necessary, probably passing it in as a variable.
<?php
$myurl = "http://asdf.com/file.txt"; // change this to meet your needs
if (preg_match("/^http:/", $myurl)) {
// code to handle
echo 'http url';
}
else if (preg_match("/^\\//", $myurl)) {
// code to handle
echo 'slash url';
}
else {
// unexpected format
echo 'unexpected url';
}
?>

Categories