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;
Related
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];
}
This question already has answers here:
PHP - parse current URL
(6 answers)
Closed 2 years ago.
How to get a part of string using PHP?
I have a string like this.
https://test-app.com/admin/api/2019-10/orders.json?limit=2&page_info=eyJpZHMiOiIyMDY3MTczMTEzOTg3LDIwNjU0ODU0MzA5MTUsMjA2NTQ3OTI3MDUzMSwyMDYyODE3MzI5MjgzIiwibGFzdF9pZCI6MjA2NTQ4NTQzMDkxNSwibGFzdF92YWx1ZSI6IjIwMjAtMDMtMTcgMTg6MTc6NTkiLCJkaXJlY3Rpb24iOiJuZXh0In0
I want only the link.. like this
orders.json?limit=2&page_info=eyJpZHMiOiIyMDY3MTczMTEzOTg3LDIwNjU0ODU0MzA5MTUsMjA2NTQ3OTI3MDUzMSwyMDYyODE3MzI5MjgzIiwibGFzdF9pZCI6MjA2NTQ4NTQzMDkxNSwibGFzdF92YWx1ZSI6IjIwMjAtMDMtMTcgMTg6MTc6NTkiLCJkaXJlY3Rpb24iOiJuZXh0In0
That's not just a string it's a URL so use URL and path functions:
$parts = parse_url($str);
echo basename($parts['path']).$parts['query'];
This question already has answers here:
PHP Parse URL From Current URL
(6 answers)
Closed 3 years ago.
How can I extract path in url using php ?
example :
text input = https://drive.google.com/open?id=1OJOZBBUCa5SsmUF3UdGJCk7A4t2kAHVr
output = 1OJOZBBUCa5SsmUF3UdGJCk7A4t2kAHVr
You can use $_GET['id'].
Or $_SERVER['QUERY_STRING'] to get all the parameters.
This question already has answers here:
PHP, Delete parts of a URL variable [duplicate]
(3 answers)
Closed 4 years ago.
I want to replace '/50' with URL.my URL is
http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=
I want url as
http://localhost/CI/admin/pcustomer/cprofile?customer=18267&mobile=&nearby=
From that little info in your question, this seems to be the simplest way to achieve "I want to replace '/50' with URL"
$url = str_replace('/50', '', 'http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=');
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]"