How to keep only this specific part of url? [duplicate] - php

This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 8 years ago.
http://www.example.com/hu/link
Using PHP how can I only keep the hu? Please note that the link is only one variable, it may be anything

You can use explode
$exploded = explode('/', 'http://www.example.com/hu/link');
$value = $exploded[3];

One solution is this:
$split = explode("/", $_SERVER["REQUEST_URI"]);
echo $split[1];

$url = "http://www.example.com/hu/link";
$split_url = explode('/', $url);
echo $split_url[3];
Output:
hu

You can use a regular expression preg_match like this:
$url = 'http://www.example.com/hu/link';
preg_match('/.*www.*\/(.*)\//i',$url, $matches);
print_r($matches[1]);

Related

PHP Remove anything after specific characters (file extensions) [duplicate]

This question already has answers here:
How to remove the querystring and get only the URL?
(16 answers)
Closed 2 years ago.
I would like to remove anything that follows after a specific set of characters (i.e. filetypes / extensions). I have tried numerous scripts I found online, but none really manage to do what I need, they either remove the file extension as well, or keep parts of the arguments that follow.
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234'
);
Desired outcome:
http://www.example.com/images/image1.jpg
http://www.example.com/images/image2.png
http://www.example.com/images/image3.jpg
http://www.example.com/images/image4.gif
http://www.example.com/images/image5.bmp
http://www.example.com/images/image6.tiff
Maybe this one help you.
$re = '/^.*(?:\.)[a-zA-Z]+/m';
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234',
'asdasd'
);
foreach ($urls as $url) {
preg_match($re, $url, $matches);
if ($matches) {
echo $matches[0];
echo "\n";
}
}
Output
http://www.example.com/images/image1.jpg
http://www.example.com/images/image2.png
http://www.example.com/images/image3.jpg
http://www.example.com/images/image4.gif
http://www.example.com/images/image5.bmp
http://www.example.com/images/image6.tiff
How about PHP's parse_url() and basename?
$inName = $urls[0]; // for example
$newName = parse_url($inName,PHP_URL_SCHEME)
. parse_url($inName,PHP_URL_HOST)
. parse_url($inName,PHP_URL_PATH)
. basename($inName);

How do i get the part of the url in php [duplicate]

This question already has answers here:
PHP get the last 3 elements of an associative array while preserving the keys?
(4 answers)
Closed 4 years ago.
How do I get Last 3 parts of the url
For example , I have link like below
http://www.yoursite/one/two/three/drink.pdf
I will get the last part of the url using below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
echo $end = end((explode('/', $url)));
But i need last 3 parts from the url like below
/two/three/drink.pdf
Please provide me a solution.
You could do this:
<?php
$url = "http://www.yoursite/one/two/three/drink.pdf";
$ex = explode('/', $url);
$arr = array_slice($ex, -3, 3);
$output = '/'.implode('/', $arr);
var_dump($output); // Outputs /two/three/drink.pdf
First, you use explode to break the url string into an array. Then use array_slice to get the last three elements of the array and finally implode to glue back the array elements using / as the glue.
Putting it all in one line would look like:
echo '/'.implode('/', array_slice(explode('/', $url), -3, 3));
Try below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
$url_array = (explode('/', $url));
print_r(array_slice($url_array,-3,3));
Hope this helps.

Preg_match_all from a url [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 7 years ago.
How can i get all the $_GET name => values from a url using preg_match_all
Example url1: http://www.example.com?go1=test1
Example url2: http://www.example.com?go2=test2
Example url3: http://www.example.com?go1=test1&go3=test3
The return need to be via array if is possible
$array = array();
$array[name1] = value1;
$array[name2] = value2;
...
How about something like:
$url = 'http://www.example.com?go1=test1&go3=test3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $array);
This will put the query string parameters in $array.
try to look $_SERVER['QUERY_STRING'] option,hopefully it will be helpful.
This may help you to get variables from url, you can also use parse_url to get a single Url components like
Using parse_url:
$url='http://www.example.com?go1=test1&go3=test3';
var_dump(parse_url($url)['query']);
See Demo :http://codepad.viper-7.com/AVFDdy
Using preg_match_all:
<?php
$re = "/\\?(.*)/";
$str = "http://www.example.com?go1=test1";
preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
See Regex :https://regex101.com/r/eB0rQ2/1

trying to getting rid of /url?q=, not working [duplicate]

This question already has answers here:
post processing on google urls
(2 answers)
Closed 8 years ago.
How can this string '/url?q=' that sits before each Google url be removed? I have tried regular expression, but it is not working.
<?php
$url = "/url?q=http://www.testinstrumentsafrica.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CBMQFjAAOGQ&usg=AFQjCNGIavZUP46nbvLPJUrDXTgC3QF6aQ";
echo preg_replace("%/url?q=%", " ", $url);
?>
You don't really need regular expresions for fixed strings:
$url = "/url?q=http://www.testinstrumentsafrica.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CBMQFjAAOGQ&usg=AFQjCNGIavZUP46nbvLPJUrDXTgC3QF6aQ";
$url = mb_substr($url, 7);
var_dump($url);
... or plain substr() if you aren't using UTF-8.
Try this:
echo preg_replace("%/url\\?q=%", " ", $url);
You need to escape ? or otherwise it represents an optional l
echo preg_replace("~\/url\?q=~", " ", $url);
The above code would replace the string /url?q= with a space.
DEMO
you could try this:
$parts = explode('/url?q=', $url);
$justUrl = $parts[0];

How can I parse just the name of the site from the URL in PHP? [duplicate]

This question already has answers here:
how to get URL host using php
(2 answers)
Closed 8 years ago.
I know that there is parse_url and then you get get the ['host'], but that returns the full www.example.com. What I want is the following:
https://stackoverflow.com/questions/ask turns to stackoverflow
https://console.aws.amazon.com/s3/home?region=us-west-2 turns to amazon
https://www.google.com/ turns to google
Any suggestions on how to do that?
Still use parse_url(), but after that, use explode() and get the 2nd to the last index
// Explode by .
$arr_host = explode('.', $host);
// Count how many in array
$count = count($arr_host);
// Get second to the last index
$domain = $arr_host[$count-2];
echo $domain;
Try
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
echo strstr($regs['domain'], '.', true);
}

Categories