This question already has answers here:
Get parts of URL in PHP
(4 answers)
Closed 1 year ago.
I had previously been using basename to grab the last part of my URL however have noticed some issues if my URL contains parameters.
So if my URL is this:
https://www.google.com/test-page/?utm_source=test
How would I pull test-page from this?
You split it by the / delimiter, and then take the fourth item
$link = 'https://www.google.com/test-page/?utm_source=test';
$split = explode('/', $link);
if(isset($split[3]))
{
echo $split[3];
}
Related
This question already has answers here:
Getting parts of a URL (Regex)
(30 answers)
Closed 3 years ago.
I have a URL like so:
http://example.com/var1/var2
What I'd like to is get VAR 1 from the querystring. I've tried using a regex but I'm not really very familiar with them. Is there an easier way?
Another approach
<?php
$e = explode("/",parse_url("http://example.com/a/b")["path"])[1];
// $e now is "a"
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 4 years ago.
I'm working on *.xml file containing pictures.
<pictures>https://images.example.co.uk/products/1835/1835776/large2/29203852.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203856.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203863.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203880.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203859.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203853.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203858.jpg|https://images.example.co.uk/products/1835/1835776/large2/29074560.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203854.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203860.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203876.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203862.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203877.jpg|https://images.example.co.uk/products/1835/1835776/large2/29074561.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203871.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203873.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203865.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203874.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203866.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203872.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203868.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203867.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203870.jpg|https://images.example.co.uk/products/1835/1835776/large2/29074558.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203879.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203864.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203878.jpg|https://images.example.co.uk/products/1835/1835776/large2/29203881.jpg</pictures>
I need to extract product ID from the first image URL https://images.example.co.uk/products/1835/1835776/large2/29203852.jpg which for this example is 1835776.
Question:
What is the correct regular expression to achieve this, considering that some elements within the image URL seperated with / are different for each product (XXXXX)?
https://images.example.co.uk/products/XXXXX/[I-WANT-THIS-ELEMENT]/large2/XXXXXXXX.jpg
Presuming the URL will always be in the same format:
$url = "https://images.example.co.uk/products/XXXXX/[I-WANT-THIS-ELEMENT]/large2/XXXXXXXX.jpg";
$url = explode('/',$url);
$id = $url[5];
echo $id;
This question already has answers here:
How to Get filename from a variable (url) in php? [duplicate]
(4 answers)
Closed 4 years ago.
I have this URL and I want to remove all characters before "blog_images/" this world.
http://localhost/login/uploads/blog_images/woman4.jpg
I have tried this PHP function it removes all words but I want to remove this word also, I need to get only this characters woman4.jpg
strstr(http://localhost/login/uploads/blog_images/woman4.jpg, 'blog_images/');
You can get file name using basename function php
<?php
/* $your_file_path_along_with_file_name is your input path */
$your_file_path_along_with_file_name = "http://localhost/login/uploads/blog_images/woman4.jpg";
$original_file_name = basename($your_file_path_along_with_file_name);
echo $original_file_name //it prints your file name
?>
you can use explode like this,
$dat = explode('blog_images/','http://localhost/login/uploads/blog_images/woman4.jpg');
echo $dat[1];
This question already has answers here:
Get URL query string parameters
(11 answers)
Closed 6 years ago.
I have a form on page, with url like this:
site.com/form.index.html?ref=https://login....
And I get page URL via this code:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
But as result I get not full URL, just this:
site.com/form.index.html
How can I get full URL?
You want to add the query string:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
Additionally, you may replace the http bit with $_SERVER[REQUEST_SCHEME]:
"$_SERVER[REQUEST_SCHEME]://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]"
This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php