I have a url that will always look like some variation of this
https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg
I need to remove with PHP the resolution specifier "-150x150" so that it reads
https://sitename/wp-content/uploads/2017/09/59a778097ae6e.jpeg
If it's always -150x150 you can just use str_replace():
$url = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$stripped = str_replace('-150x150', '', $url);
var_dump($stripped);
// string(62) "https://sitename/wp-content/uploads/2017/09/59a778097ae6e.jpeg"
If you need a way to strip out any resolution, you can use a regular expression for that:
$url = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$stripped = preg_replace('/-[0-9]+x[0-9]+/', '', $url);
var_dump($stripped);
// string(62) "https://sitename/wp-content/uploads/2017/09/59a778097ae6e.jpeg"
hello you can use strpos() and substr() functions
<?php
$str1 = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$str2 = "-150x150";
$pos = strpos($str1, $str2);
$part1 = substr($str1, $pos);
$part2 = substr($pos+1, strlen($str1));
$final_str = $part1.$part2;
echo $final_str;
?>
or you can also just use str_replace() and replace the part of the url by nothing :
<?php
$url = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$str = "-150x150";
// will replace $str by '' in $url
$url = str_replace($str, '', $url);
echo $url;
?>
If it's not always 150x150, here's a nifty solution.
$url = 'https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg';
First get the extension
$ext = explode('.', $url);
$ext = $ext[count($ext)-1];
Then split by '-'
$array = explode('-', $url);
Pop the last array element which will be the resolution (150x150 here)
array_pop($array);
Then implode by '-' again and concatenate the extension to the new url
$new_url = implode('-', $array). '.' .$ext;
Related
How can I keep a part from a string:
$str = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
I want to keep all after /_hd/.
I try with this but it keeps the hd/:
echo substr($str, strpos($str, '_hd/') + 1);
// hd/791df3a1355efd3.jpg
Thanks.
you could simply use pathinfo method which will parse your path and return an array like this
array(4) {
["dirname"]=>
string(37) "../assets/uploads/8b3da36c4bce050/_hd"
["basename"]=>
string(19) "791df3a1355efd3.jpg"
["extension"]=>
string(3) "jpg"
["filename"]=>
string(15) "791df3a1355efd3"
}
for you what you are looking for will be called basename
$path = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
echo pathinfo($path)['basename']; // 791df3a1355efd3.jpg
Considering the example string witch appears to be a path and also assuming it is a single line, I propose a couple of examples with the first preferred.
<?php
$str = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
echo basename($str);
This will output 791df3a1355efd3.jpg
<?php
$str = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
echo preg_replace('#^.+\/#', '', $str);
This will output 791df3a1355efd3.jpg
With the second example if you also wanted to make sure /_hd/ is in the string
<?php
$str = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
echo preg_replace('#^.+\/_hd\/#', '', $str);
and to get an array of values checking if /_hd/ is in the string (you can use basename() instead of preg_replace())
<?php
$str = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
$files = array();
if (preg_match('#\/_hd\/#', $str)) {
$files[] = preg_replace('#^.+\/_hd\/#', '', $str);
}
var_dump($files);
echo is for testing but you can assign the result to a variable instead in both cases.
You can use the explode function
<?php
$str = '../assets/uploads/8b3da36c4bce050/_hd/791df3a1355efd3.jpg';
$explode = explode('/',$str);
echo $result = $explode['5'];
?>
You may try this, for example
$name = substr($str, strrpos($str, '/', -1) + 1);
or
$items = explode($str, '/');
$name = $items[count($items) - 1];
I have a string $current_url that can contain 2 different values:
http://url.com/index.php&lang=en
or
http://url.com/index.php&lang=jp
in both cases I need to strip the query part so I get: http://url.com/index.php
How can I do this in php?
Thank you.
Simplest Solution
$url = 'http://url.com/index.php&lang=en';
$array = explode('&', $url);
echo $new_url =$array[0];
To only remove the lang query do this
$url = 'http://url.com/index.php&lang=en&id=1';
$array = explode('&lang=en', $url);
echo $new_url = $array[0] .''.$array[1];
//output http://url.com/index.php&id=1
So this way it only removes the lang query and keep other queries
If the value of your lang parameter is always of length 2, which should be the case for languages, you could use:
if(strpos($current_url, '&lang=') !== false){
$current_url = str_replace(substr($current_url, strpos($current_url, '&lang='), 8), '', $current_url);
}
If the substring "&lang=" is present in $current_url, it removes a substring of length 8, starting at the "&lang=" position. So it basically removes "&lang=" plus the 2 following chars.
You can Use strtok to remove the query string from url.
<?php
echo $url=strtok('http://url.com/index.php&lang=jp','&');
?>
DEMO
Answer based on comment.
You can use preg_replace
https://www.codexworld.com/how-to/remove-specific-parameter-from-url-query-string-php/
<?php
$url = 'http://url.com/index.php?page=site&lang=jp';
function remove_query_string($url_name, $key) {
$url = preg_replace('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', "$1", $url_name);
$url = rtrim($url, '?');
$url = rtrim($url, '&');
return $url;
}
echo remove_query_string($url, 'lang');
?>
DEMO
I have make a try like this:
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-13);
and the output work :
localhost/product/-/123456 cause this just for above link with 13 character after /-/123456
How to remove all? i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-(.*));
not work and error sintax.
and i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-999);
the output is empty..
Assume there are no number after localhost/product/-/123456, then I will just trim it with below
$string = "localhost/product/-/123456-Ebook-Guitar";
echo rtrim($string, "a..zA..Z-"); // localhost/product/-/123456
Another non-regex version, but require 5.3.0+
$str = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo dirname($str) . "/" . strstr(basename($str), "-", true); //localhost/product/-/123456
Heres a more flexibility way but involve in regex
$string = "localhost/product/-/123456-Ebook-Guitar";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
$string = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
This should match capture everything up to the number and remove everything afterward
regex101: localhost/product/-/123456-Ebook-Guitar
regex101: localhost/product/-/123456-Ebook-Guitar-1-pdf/
Not a one-liner, but this will do the trick:
$string = "localhost/product/-/123456-Ebook-Guitar";
// explode by "/"
$array1 = explode('/', $string);
// take the last element
$last = array_pop($array1);
// explode by "-"
$array2 = explode('-', $last);
// and finally, concatenate only what we want
$result = implode('/', $array1) . '/' . $array2[0];
// $result ---> "localhost/product/-/123456"
I have the following url : http:example.com/country/France/45.
With the pattern http:example.com/country/name/**NUMBER**(?$_GET possibly).
How can I extract the number with a regex (or something else then regex) ?
With regexp:
$str = 'http:example.com/country/France/45';
preg_match('/http:example\.com\/country\/(?P<name>\w+)\/(?P<id>\d+)/', $str, $matches);
print_r($matches); // return array("name"=>"France", "id" => 45);
$url = 'http:example.com/country/France/45';
$id = end(explode('/',trim($url,'/')));
Simple isn't ?
The usage of trim () is to remove trailing \
echo $last = substr(strrchr($url, "/"), 1 );
strrchr() will give last occurence of the / character and then substr() gives string after it.
use something like this
$url = "http://example.com/country/France/45";
$parts = explode('/', $url);
$number = $parts[count($parts) - 1];
and if you have GET variable at the end, you can explode further like this
$number = explode('?', $number);
$number = $number[0];
hope this helps :)
the get command for php is $_GET so to show to number do
<html>
<body>
<?php
echo $_GET["eg"];
?>
</body>
</html>
with a URL of http:example.com/country/name/?eg=**NUMBER**
Use explode():
$parts = explode('/', $url);
$number = $parts[count($parts)-1];
Can someone help me with replacing this url
http://www.izlesene.com/video/arabic-shkira-belly-dance/200012
to: 200012
$url = 'http://www.izlesene.com/video/arabic-shkira-belly-dance/200012';
$out = preg_replace("/[^0-9]/i","",$url);
or
preg_match("/\/([0-9]+)/i",$url,$m);
$out = $m[1];
use the basename
$video_id = basename($url);
var_dump($video_id);
or
try to explode and get the last item.
$url = "http://www.izlesene.com/video/arabic-shkira-belly-dance/200012";
$segments = explode("/",$url);
$video_id = end($segments);
var_dump($video_id);
If things works like in JS you can do it like so:
$url = "http://www.izlesene.com/video/arabic-shkira-belly-dance/200012";
$a = explode("/", $url);
$number = array_pop($a);
And maybe like this:
$url = "http://www.izlesene.com/video/arabic-shkira-belly-dance/200012";
$number = array_pop( explode("/", $url) );
Or you can do it faster without arrays and regex:
$url = "http://www.izlesene.com/video/arabic-shkira-belly-dance/200012";
$url = substr($url, strrpos($url, "/") + 1);
strrpos searches the position of the last occurrence of / in a string and substr returns the portion of a string specified by this position.
Try:
$number = basename($url);
Hint: Regular expressions are not always the best solution (even if quite powerful but also complicated). See basenameDocs.
If you really need a regex, start at the end:
$url = "http://www.izlesene.com/video/arabic-shkira-belly-dance/200012";
$number = preg_replace('~.*/([^/]+)$~', '$1', $url);
^