RE: [PHP] Regex Assistance
| From: | Scott Hurring | Date: | Tue, 28 May 2002 17:13:29 +0000 |
| Subject: | RE: [PHP] Regex Assistance | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-99514@lists.php.net to get a copy of this message | ||
This should work for most cases:
function strip_key($key, $string)
{
$string = preg_replace("/(&|\?)($key=(\w+)?)(&?)/", '$1', $string);
return preg_replace('/(\?|&)$/', '', $string);
}
print strip_key("list", "yadda?bo=ra&list=XXX&two=three");
print strip_key("list", "yadda?bo=ra&list=&two=three");
print strip_key("list", "yadda?list=XXX");
print strip_key("list", "yadda?list=XXX&two=three");
print strip_key("list", "yadda?no=where&list=XXX");
---
Scott Hurring
Systems Programmer
EAC Corporation
ScottH@eac.com
Voice: 201-462-2149
Fax: 201-288-1515
> -----Original Message-----
> From: Scott Reismanis [mailto:areisman@bigpond.net.au]
> Sent: Monday, May 27, 2002 10:00 PM
> To: php-general@lists.php.net
> Subject: [PHP] Regex Assistance
>
>
> Hey All,
>
> I am just begining to learn regex functions in PHP. Anyhow I
> am trying
> to code a preg_replace function which basically cleans a URL.
>
> What I mean is say a url is index.php?page=hello&list=10&start=4 you
> pass that URL and say 'list' to the function (shown below) Anyhow I
> want that to then return index.php?page=hello&start=4 (see how the
> entire list reference is now gone)
>
> Here is what I have come up with so far, though I am yet to
> test it as
> I am coding this at work :)
>
> function stripUrl($url, $url_strip)
> {
> // Check that $url_strip is in the URL
> if(strpos($url,'?') && strpos($url, $url_strip.'='))
> {
> // Lets clean up the url
> $url = preg_replace("/\?|&$url_strip=/", "", $url);
> }
> return $url;
> }
>
> My problem is that, how do I make the preg_replace stop when it
> encounters a & in the URL? If anyone could shed some light on this it
> would be appreciated,
>
> - Regards, Scott
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>