Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

PHP Strip

How would I strip &expires=5180531 from the following string in php.  The length and numerical value can change.

CAAWhyYsjaWsBAOCwjZCsZC4TMZBlYwMF76RkK0XvX6QP2vuOWsxCSD6u9dTQtU2xHm70t9652qvFKAmYhkHnSQX3MOlAdOI9rvZBJTM959rWWXsTt3AZBINv6zcRS6y5kx19Wdl5J8uiJLTM2nBPW1xVwOfT4E4OARNZA9KlbhGuJwgYLASpMiqloke9DQhe4ZD&expires=5180531

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

$str='CAAWhyYsjaWsBAOCwjZCsZC4TMZBlYwMF76RkK0XvX6QP2vuOWsxCSD6u9dTQtU2xHm70t9652qvFKAmYhkHnSQX3MOlAdOI9rvZBJTM959rWWXsTt3AZBINv6zcRS6y5kx19Wdl5J8uiJLTM2nBPW1xVwOfT4E4OARNZA9KlbhGuJwgYLASpMiqloke9DQhe4ZD&expires=5180531';

$result = preg_replace('/&?expires=\d+/','', $str);

echo $result;

Open in new window

You can use PHP explode() function and "&expires=" as delimiter:
$str = "CAAWhyYsjaWsBAOCwjZCsZC4TMZBlYwMF76RkK0XvX6QP2vuOWsxCSD6u9dTQtU2xHm70t9652qvFKAmYhkHnSQX3MOlAdOI9rvZBJTM959rWWXsTt3AZBINv6zcRS6y5kx19Wdl5J8uiJLTM2nBPW1xVwOfT4E4OARNZA9KlbhGuJwgYLASpMiqloke9DQhe4ZD&expires=5180531";
$result = explode("&expires=", $str)[0];
echo $result;

Open in new window

The advantage of using a regular expression in this case goes to the nature of URLencoded arguments.  The argument string is typically prepended with a question mark, and separated with ampersands.  You cannot assume that they are presented in any particular order; the "expires=" parameter might be first or last, and should have the same meaning no matter where it appears in the string.  There are exceptions, of course, for duplicate arguments, but most of the time, you want your script to work correctly no matter where any parameter appears in the unparsed argument string.

PHP also has functions for building and parsing URL parameters.  My instinct would be to check these out, too.  They might be easier than regular expressions.
http://us2.php.net/manual/en/function.http-build-query.php
http://us2.php.net/manual/en/function.parse-str.php
http://us2.php.net/manual/en/function.parse-url.php
To anyone coming across this question in the future, I would recommend that you test the solutions carefully before you consider using one of them in a deployed application.  Not all of them will have the desired functionality at all times, and at least two of the accepted solutions propose potential time-bombs that could pass unit testing but fail in a deployed application.  Buyer beware.  Here is one example of the potential risk.
<?php 
/**
 * http://www.experts-exchange.com/questions/28698807/PHP-Strip.html
 */
error_reporting(E_ALL);
echo '<pre>';

// IN THIS EXAMPLE, FAILURE TO REMOVE THE ARGUMENT
$str = "URL?expires=5180531&foo=bar";
$result = explode("&expires=", $str)[0];
echo PHP_EOL . $result;

// IN THIS EXAMPLE, SUCCESS: REMOVE THE ARGUMENT
$str = "URL?foo=bar&expires=5180531";
$result = explode("&expires=", $str)[0];
echo PHP_EOL . $result;

// IN THIS EXAMPLE, BIZARRE OUTPUT WITH DATA LOSS
$str = "URL?foo=bar&expires=5180531&abc=DEF";
$result = explode("&expires=", $str)[0];
echo PHP_EOL . $result;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nathan Riley

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for Nathan Riley's comment #a40893243
Assisted answer: 125 points for hielo's comment #a40890899
Assisted answer: 125 points for Brian Tao's comment #a40890955
Assisted answer: 125 points for Ray Paseur's comment #a40891283
Assisted answer: 125 points for Nrupendra Nath Panigrahi's comment #a40892024

for the following reason:

I accepted them as solutions as they appeared to do the job, although I went with a solution on my own.

I was stripping the "access_token" field off the front before it even got to this point, which after some research found parse_str would work better if I didn't strip.  So my solution I ended up with is below.
Just closing this out, already stated.