Nokki Posted June 7, 2009 Share Posted June 7, 2009 Hello, I am pretty much new to php and I am trying to figure out the following: I have a URL string like: domain.com/index.jsp;jsessionid=824E485D748EB687F88497BA39685C81 I am looking for someone who could help me out with a function that would be like function cleanurl($word,$toclean) { } The idea would be that I can use it like: $cleanurl = cleanurl($dirtyurl,";jsessionid"); So what the function would do is: Check the url for ;jsessionid and remove everything after, but including the selected word / mini string. Here is an example: $dirtyurl = "domain.com/index.jsp;jsessionid=824E485D748EB687F88497BA39685C81"; $cleanurl = cleanurl($dirtyurl,";jsessionid"); echo $cleanurl woudl return: domain.com/index.jsp Any help would be great Link to comment https://forums.phpfreaks.com/topic/161265-solved-remove-end-of-string-after-specific-word/ Share on other sites More sharing options...
Adam Posted June 7, 2009 Share Posted June 7, 2009 It's a lot simpler than you might think with the substr_replace() function. Try this: function cleanurl($word, $toclean) { $pos = strpos($word, $toclean); if ($pos !== false) { return substr_replace($word, '', $pos); } return $word; } Link to comment https://forums.phpfreaks.com/topic/161265-solved-remove-end-of-string-after-specific-word/#findComment-850955 Share on other sites More sharing options...
Nokki Posted June 7, 2009 Author Share Posted June 7, 2009 Thanks for that - it works fine so far. I wonder if there is a different way as I just noticed that some urls break after removing the session ids if the urls are like domain.com/index.php?PHPSESSID=12345&area=home Is there maybe a way to just strip from the startpoint to either the next slash (/) or to the next "and" (&) so values after the removed session stay intact? Again, thanks for your very quick post - It's nearly working as expected Link to comment https://forums.phpfreaks.com/topic/161265-solved-remove-end-of-string-after-specific-word/#findComment-850974 Share on other sites More sharing options...
Adam Posted June 7, 2009 Share Posted June 7, 2009 You can do this easily with PEAR: <?php require_once 'Net/URL.php'; $dirtyurl = 'domain.com/index.php?PHPSESSID=12345&area=home'; $url = new Net_URL($dirtyurl); unset($url->querystring['PHPSESSID']); $cleanurl = $url->getURL(); ?> Although you may want to add "http://" to the start of your URLs to stop PEAR automatically adding the current domain you're on... Link to comment https://forums.phpfreaks.com/topic/161265-solved-remove-end-of-string-after-specific-word/#findComment-850985 Share on other sites More sharing options...
thebadbad Posted June 7, 2009 Share Posted June 7, 2009 Or using regular expressions: <?php function cleanurl($url, $query_name) { $pattern = '~(\?|&)' . preg_quote($query_name, '~') . '=[^&]*&?~'; $cleanurl = preg_replace($pattern, '$1', $url); if (in_array(substr($cleanurl, -1), array('?', '&'))) { $cleanurl = substr($cleanurl, 0, -1); } return $cleanurl; } $url = 'http://domain.com/index.php?PHPSESSID=12345&area=home'; echo cleanurl($url, 'PHPSESSID'); //http://domain.com/index.php?area=home ?> It should return a perfectly valid URL, removing ?s and &s when necessary. If your URLs use a semi-colon instead of a question mark to denote the start of the query string, you can swap the first and last question mark within the function with semi-colons. Link to comment https://forums.phpfreaks.com/topic/161265-solved-remove-end-of-string-after-specific-word/#findComment-851029 Share on other sites More sharing options...
Nokki Posted June 10, 2009 Author Share Posted June 10, 2009 Thank you for all your answers - I used a mix of all the above functions Again, thanks a lot Link to comment https://forums.phpfreaks.com/topic/161265-solved-remove-end-of-string-after-specific-word/#findComment-853023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.