Remove Unwanted Gap from PHP-Generated URL [duplicate] - php

This question already has answers here:
Removing a space
(4 answers)
Closed 9 years ago.
This script sends instructions to delete a user from a database when a php-generated link is clicked.
<?php
$user = $_GET['user'];
$zip = $_GET['zip'];
$url = "<p>http://domain.org/remove.php?do=delete";
$url .= "&removeid=".$user."&rcrd=".$zip."</p>";
echo $url;
?>
But the generated link has an unwanted space after "&removeid=" producing the following:
http://domain.org/remove.php?do=delete&removeid= 160294&rcrd=41233
The link is, therefore, unclickable.
Suggestions appreciated.

use the function trim - http://php.net/manual/en/function.trim.php
trim($user);

Related

Get portion of URL with PHP [duplicate]

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

How to extract specific value between slashes in a URL? [duplicate]

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;

Remove character from image url [duplicate]

This question already has answers here:
How to remove the querystring and get only the URL?
(16 answers)
Closed 8 years ago.
I'm new to PHP and WordPress, and i need to import image from a offshore website with the WordPress function download_URL().
My problem is, i have URL that look like that :
http://www.test/img/FMR.jpg
But i have also URL that look like that :
http://www.test/img/FMR.jpg?Qq_0y12h
And can i have remove everything that is after the .extension if there something?
you can use preg_replace, I did this little example for you.
<?php
$url = "http://www.test/img/FMR.jpg?Qq_0y12h";
echo "url = $url\n\n";
$urlFormatted = preg_replace("/\?.*$/", "", $url);
echo "urlFormatted = $urlFormatted\n";
?>

Remove url from string using PHP [duplicate]

This question already has answers here:
Get path from URL
(2 answers)
Closed 8 years ago.
Can anyone please tell me how to remove url from a string using PHP
I have this string
src="http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
Desired Output:
src="/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
You can use the parse_url() buil-in php function for this.
For example:
<?php
$url = 'http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png';
$parsed_url = parse_url($url);
var_dump($parsed_url);
?>
Output:
So, after the url is parsed, the bit you want is found on $parsed_url['path']. Hope this helps.

How to extract last part in PHP? [duplicate]

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

Categories