I need to delete all characters to the last "/"
This is my url:
http://www.example.com/highlights/cat/all-about-clothing/
And I want to have only:
all-about-clothing
thanks
Use basename ()
$str = 'http://www.example.com/highlights/cat/all-about-clothing/'; echo basename($str); // Outputs: all-about-clothing
EDIT:
Another solution:
$str = 'http://www.example.com/highlights/cat/all-about-clothing/'; $path = pathinfo($str, PATHINFO_BASENAME); echo "<br/>" . $path;
Use the PHP function parse_url () .
edit: basename()or pathinfo()is an easier way.
basename()
pathinfo()
$str = 'http://www.example.com/highlights/cat/all-about-clothing/'; $str = trim($str,'/'); $str = explode('/',$str); echo $str = end($str);
//
-
<?php $url = "http://www.example.com/highlights/cat/all-about-clothing/"; $url_path = parse_url($url, PHP_URL_PATH); $basename = pathinfo($url_path, PATHINFO_BASENAME); echo $basename; ?>
regex:
$match = []; $subject = 'http://www.example.com/highlights/cat/all-about-clothing/'; $pattern = '/http:\/\/www\.example\.com\/highlights\/cat\/(.*)/'; preg_match($pattern, $subject, $match); print_r($match);
.
<?php $url = 'http://www.example.com/highlights/cat/all-about-clothing/'; $basename = split('/',$url); echo $basename[5]; ?>
URL- , , , , :
http://www.example.com/highlights/cat/all-about-clothing/?page=1 http://www.example.com/highlights/cat/all-about-clothing/item/1
to cache the third โdirectoryโ after the domain name and ignore the rest of the URL, you can use the following code:
$url = "http://www.example.com/highlights/cat/all-about-clothing/item/1"; $url_path = parse_url($url, PHP_URL_PATH); # /highlights/cat/all-about-clothing/item/1 $dirs = explode('/', $url_path); # Array([0] =>"", [1]=>"highlights", [2]=>"cat", [3]=>"all-about-clothing", [4]=>"item", [5]=>"1") echo $dirs[3]; # all-about-clothing
Source: https://habr.com/ru/post/1614264/More articles:Failed to load Microsoft.Kinect.Tools file or assembly - c #OBIEE 11g Users are not visible in Identity Manager in the Administration Tool that are created in Weblogic - obieeUnable to stop thread in tweepy after one minute - pythonUser-mapping.xml does not work in guacamole, invalid login - javaRendering error after AJAX call - redirectHow to add / design vectors iTextSharp - vectorJavascript Instant Search Feature - javascriptSQL Server 08 - pivot - Change column name - sql-serverThe Postgres index has a significant impact on the original SQL query and ActiveRecord - ruby-on-railsInsert multiple rows across 3 tables in 1 query using the returned identifier - sqlAll Articles