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];
?>
Related
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
I have this type of urls stored in a php variable:
$url1 = 'https://localhost/mywebsite/help&action=something';
$url2 = 'https://localhost/mywebsite/jobs&action=one#profil';
$url3 = 'https://localhost/mywebsite/info&action=two&action2=something2';
$url4 = 'https://localhost/mywebsite/contact&action=one&action2=two#profil';
I want to replace the page help, jobs, info, contact with home in a very simple way, something like this:
echo replaceUrl($url1);
https://localhost/mywebsite/home&action=something
echo replaceUrl($url2);
https://localhost/mywebsite/home&action=one#profil
echo replaceUrl($url3);
https://localhost/mywebsite/home&action=two&action2=something2
echo replaceUrl($url4);
https://localhost/mywebsite/home&action=one&action2=two#profil
So here is the solution i found:
function replaceUrl($page){
$pieces = explode("/", $page);
$base = '';
for ($i=0; $i<count($pieces)-1; $i++) $base .= $pieces[$i].'/';
$hash = strpbrk($pieces[count($pieces)-1], '&#');
return $base.'home'.$hash;
}
You'll want to add something like
RedirectMatch 301 help(.*) home$1
to your .htaccess file. I'm not sure PHP is the correct tool for the job.
If you actually want to modify a string with the value of that (which is what your tags and.. comments indicate), you'll want to do:
$url = "https://localhost/mywebsite/help&action=something#profil"
$url = str_replace("help", "home", $url);
echo $url; // https://localhost/mywebsite/home&action=something#profil
In my prestashop shop i have fetched the current web page url by using the below php code.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
My current echo url is http://shoppingworld.com/int/Mens-Tshirts/Fashion.html
My shop url is http://shoppingworld.com/int/
I need to remove the url portion which is coming next to the above shop url.
Try this
<?php
$url_path="http://www.shoppingworld.com/int/Mens-Tshirts/Fashion.html";
$a = parse_url($url_path, PHP_URL_SCHEME);
$b = parse_url($url_path, PHP_URL_HOST);
$url_name_parse=explode('/',$url_path);
$url_name=$url_name_parse[3];
echo ($a . "://" . $b .'/' .$url_name.'/'); ?>
Program Output
http://www.shoppingworld.com/int/
DEMO
You can't directly get that partial url.
Try this,
$url = 'http://shoppingworld.com/int/Mens-Tshirts/Fashion.html';
$parsed = parse_url($url);
$path_array = explode('/', $parsed['path']);
echo $parsed['scheme'] . '//' . $parsed['host'] .'/'. $path_array[1] . '/';
Demo
$arr_url = parse_url($url);
$host = $arr_url['host'];
$service_uri = $arr_url['path'];
read more in php manual about parse_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.
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.