Remove part URL before second last slash in PHP - php

How can I remove the part in my URL that is before the second last slash?
My URL is this:
http://antwerp.test/nl/aanbod/activiteiten/kunst/the-world-of-steve-mccurry/the-world-of-steve-mccurry/activity-mccurry/
So I want to remove "the-world-of-steve-mccurry/" the second time it appears. The URL should be:
http://antwerp.test/nl/aanbod/activiteiten/kunst/the-world-of-steve-mccurry/activity-mccurry/

you can explode the url by "/" , then remove the mentioned part , then implode again :
function sms($url)
{
substr($url,-1) === "/"? $position = 3:$position = 2; // decide if URL ends with `/` or not
$partsArray = explode('/',$url);
$partsArrayCount = count($partsArray);
$toDelete = $partsArray[$partsArrayCount-$position]; // get the part that you want to delete
unset($partsArray[$partsArrayCount-3]);
$result = implode("/",$partsArray);
return $result;
}

Related

Remove Path from PHP String front and back

i have bellow string in php and i want remove directory listing and get only the xml name
/usr/local/EDI/GTNexus/BookingConfirmation/test/PROCESSED/ELITESGGTN_30120180814_1410366355.XML_20180817135443
i want to return the string as ELITESGGTN_30120180814_1410366355.XML.
i use below way but im not getting the expected result
to remove front i used this but not getting the expected outcome
$newfname = strstr($fname, '/');
You can explode on "/" and then find the last undescore from the last item and substring it.
$str = "/usr/local/EDI/GTNexus/BookingConfirmation/test/PROCESSED/ELITESGGTN_30120180814_1410366355.XML_20180817135443";
$arr = explode("/", $str);
$name = substr(end($arr), 0, strrpos(end($arr), "_"));
echo $name; //ELITESGGTN_30120180814_1410366355.XML

PHP Regular expression replace for url

I need help to replace the last occurance of numeric uri string from my url:
For eg I have url strings as:
$url1 = "http://localhost/mystore/electronics/company/5-mobile/5";
$url2 = "http://localhost/mystore/electronics/company/5-mobile/21";
$url3 = "http://localhost/mystore/electronics/company/5-mobile";
from which I want to remove last occurance of "/5" or "/21" or any other number following the last forward slash with empty string and get only:
$url1_after_preg_replace = "http://localhost/mystore/electronics/company/5-mobile";
$url2_after_preg_replace = "http://localhost/mystore/electronics/company/5-mobile";
$url3_after_preg_replace = "http://localhost/mystore/electronics/company/5-mobile";
Thanks in advance for any help.
Not using preg replace but a simple way :
if(explode(dirname($url1),$url1)[1] != "/5-mobile") {
$url1_after_preg_replace = dirname($url1);
}
Reference :
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.explode.php

Detect language in my url

I have a url and I want to detect the language to save it in the cookies.
This is my url :
https://www.example.com/en/test.php
There are different methods to do it:
1. Method A (explode REQUEST_URI):
echo $_SERVER['REQUEST_URI']; // /en/test.php
$exp = explode('/',$_SERVER['REQUEST_URI']); // explode by slash
$language = $exp[1]; // first element before / (slash)
Get the current url (without serveur) :
$url = $_SERVER['REQUEST_URI']; // gives "/en/test.php"
Then explode it in an array :
$urlParts = explode ('/', $url); // split the url by /
The first element (index 0) is empty (because the string starts with /), and the language is the second one (index 1)
$language = $urlParts[1] ;
Don't forget to check at each steps if the url is OK and the arrayx contains more than 1 element.

PHP - How to remove pagination page variable from URL?

I am tryign to remove pagination page ID from URL. For example I have URL looks like this:
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
And want to remove, If any of matches match from URL:
1. page=1
2. page=1&page=2
3. page=1&page=2$page=3
4. page=1&page=2$page=3$page=4
In my current pagination code, Previous page is concating everytime when page is changed thats why I want to remove all page.
I have used this code but not working well.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
//$getUrl =$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$parts = parse_url($urlVal);
$urlVar = "";
$urlVar = $parts['query'];
echo $urlVar = preg_replace('/page=[0-9]&+/', '', $urlVar);
Try my code here: http://codepad.org/lSHV7a7w
I hope you understand what I am trying to do.
Thanks.
Try this.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
$parts = parse_url($urlVal);
parse_str($parts['query'], $query);
unset($query['page']);
$newUrl = "{$parts['scheme']}://{$parts['host']}{$parts['path']}?" . http_build_query($query);
echo $newUrl;
// Regx Pattern
echo preg_replace('/&?page=[0-9]+&?/', '', $parts['query']);
How about using concatenation of numbers as page value?
$urlVal = "http://192.168.1.233/sitename/property-list?page=11,13&etc..."
It shortens url and makes removing parameter easy:
$urlVar = preg_replace('/page=[0-9,]+&/', '', $urlVar);
You can extract them from url using:
preg_match("/page=([0-9,]+)&/", $urlVal, $m);
$pages = explode(",", $m[1]);

php : Retrieving number in the URL

I have url in the following types
http://domain.com/1/index.php
http://domain.com/2/index.php
http://domain.com/3/index.php
http://domain.com/4/index.php
I need to retrieve only the number from the url .
for example,
when i visit http://domain.com/1/index.php , It must return 1.
Have a look at parse_url.
$url = parse_url('http://domain.com/1/index.php');
EDIT: Take a look at $_SERVER['REQUEST_URI'], to get the current URL. Use that instead of $url['path'].
Then you can split $url['path'] on /, and get the 1st element.
// use trim to remove the starting slash in 'path'
$path = explode('/', trim($url['path'], '/'));
$id = $path[0]; // 1
Given the information provided, this will do what you ask... it's not what I would call a robust solution:
$url = $_SERVER['PATH_INFO']; // e.g.: "http://domain.com/1/index.php";
$pieces = explode("/", $url);
$num = $pieces[3];
split the server path by forward slashes (explode('/', $_SERVER['REQUEST_PATH']);)
remove empty entry from the beginning
take the first element
make sure it is an integer (intval(), or a simple (int) cast).
No need to use regular expressions for this.
You use preg_match() to match the domain and get the 1st segment.
$domain = http://www.domain.com/1/test.html
preg_match("/http:\/\/.*\/(.*)\/.*/", "http://www.domain.com/1/test.html");
echo $matches[1]; // returns the number 1 in this example.

Categories