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);
^
Related
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 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;
I have a string which contains a url. I am trying to extract the url from the additional text in the most efficient way. So far I have been using explode but I have to explode twice and then rebuild the url. Regex is not something I dominate yet so i placed it out of the question(unless it is the best solution). Is there a way to extract the url in one step?
$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$strip1 = explode( '&', $url );
$strip2 = explode('=', $strip1[0]);
$result = $strip2[1].'='.$strip2[2];
result:
http://www.somesite.com/sites/pages/page?id=1545778
Try like this:use preg_split()
$date = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$t =preg_split("/[=&]/", $date);
echo $t[1]."=".$t[2]; //output: http://www.somesite.com/sites/pages/page?id1545778
$strip1 = explode( '/url?q=', $url );
Use this regex $strip1
^((http):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$
you will get an array of sections in the url
Ugly one-step regex-free solution.
$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$result = substr( $url, strpos( $url, '=' ) + 1, strpos( $url, '&' ) - strpos( $url, '=' ) - 1 );
echo $result;
And cleaner two-step variation.
$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$start = strpos( $url, '=' ) + 1;
$result = substr( $url, $start, strpos( $url, '&' ) - $start );
echo $result;
Somewhat less-ugly regex solution.
$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$result = preg_replace( '/[^=]*=([^&]*).*/', '${1}', $url );
echo $result;
Both produce the following output.
http://www.somesite.com/sites/pages/page?id=1545778
Technically, that second ? in the URL should be URL encoded, but we can get around that. Use parse_url to get the query, then replace ? with a URL encoded version using str_replace. After this, you will have a valid query that you can parse using parse_str.
$query = parse_url($url, PHP_URL_QUERY);
$query = str_replace("?", urlencode("?"), $query);
parse_str($query, $params);
echo $params['q'];
// displays http://www.somesite.com/sites/pages/page?id=1545778
$url = "/url?q=http://www.somesite.com/sites/pages/page?id=1545778&sa=U&ei=EhHLVL_yJcb-yQSZ7oDgAg&ved=0CBMQFjAA&usg";
$strip3 = current(explode('&',end(explode('=', $url,2))));
print_r ($strip3); //output http://www.somesite.com/sites/pages/page?id=1545778
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];
how to get id url with preg_replace.
this is the link:
http://www.DDDD.com.br/photo/5b87f8eaa7c20f79c3257eb3ec0a35e0/id how do I get the id? in the case would be: 5b87f8eaa7c20f79c3257eb3ec0a35e0
In this case I recommend not to use preg_match (preg_replace would be used to replace something.
Simply use
$array = explode('/',$_SERVER['REQUEST_URI']);
$id = $array[1];
If you must use preg_match:
$array = array();
preg_match('#^/photo/([0-9a-f]{32})/id$#',$_SERVER['REQUEST_URI'],$array);
$id = $array[1];
You can do this easily using strripos to find the last / in the URL.
$url = $_SERVER['REQUEST_URI'];
if (($pos = strripos($url, '/')) !== false) {
$id = substr($url, $pos + 1);
}
else {
trigger_error('You must supply a valid photo ID');
}
If you would like to just extract that id string, you can use:
$id_url = "http://www.DDDD.com.br/photo/5b87f8eaa7c20f79c3257eb3ec0a35e0/id";
$pattern = "/photo\/([a-zA-Z0-9]*)/";
preg_match($pattern, $id_url, $output_array);
echo $output_array[1];
Or, to make the replacement:
$id_url = "http://www.DDDD.com.br/photo/5b87f8eaa7c20f79c3257eb3ec0a35e0/id";
$pattern = "/photo\/([a-zA-Z0-9]*)/";
$replacement = "your replacement";
$replaced_url = preg_replace($pattern, $replacement, $id_url);
echo $replaced_url;
PHP Live Regex - a useful tool for testing your patterns