Remove all the word before a specific word [duplicate] - php

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

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

String Function in PHP [duplicate]

This question already has answers here:
PHP - Return everything after delimiter
(7 answers)
How to get everything after a certain character?
(9 answers)
get substring after delimiter
(3 answers)
Get the string after a string from a string
(5 answers)
Closed 3 years ago.
String Function in PHP:
$img = "WhatsApp Image 2019-11-18 at 17.15.42.jpeg | xyz.com/content/uploads/wcpa_uploads/image.jpg";
using string functions, how can I get complete text after | symbol, I mean only the complete url of image should be saved in variable.
There are several methods for that. If you do not want to regex or split to array, try this:
$path = trim(substr(strstr($img, "|"), 1));
Simplest way:
trim(explode('|', $img)[1])

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;

How do I get the end of a file path in php? [duplicate]

This question already has answers here:
How to get the last dir from a path in a string
(11 answers)
Closed 6 years ago.
So I have a file path as a text variable in php:
"one/cool/file/path"
I need a php code to return "path"
By return I think you mean display...
Lets suppose you have the following path:
"one/cool/file/path"
Stored as a string in the variable:
"path"
To return it (or display it) you can just do:
echo path;
If you then go to your php in a web browser you should see your parh there!
You could achieve this using the following method:
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
Output: /www/htdocs/inc
http://php.net/manual/en/function.pathinfo.php#example-2657

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