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

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
$str='CAAWhyYsjaWsBAOCwjZCsZC4TMZBlYwMF76RkK0XvX6QP2vuOWsxCSD6u9dTQtU2xHm70t9652qvFKAmYhkHnSQX3MOlAdOI9rvZBJTM959rWWXsTt3AZBINv6zcRS6y5kx19Wdl5J8uiJLTM2nBPW1xVwOfT4E4OARNZA9KlbhGuJwgYLASpMiqloke9DQhe4ZD&expires=5180531';

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

echo $result;

Open in new window

3
Brian TaoSenior Business Solutions ConsultantCommented:
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

0
Ray PaseurCommented:
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Ray PaseurCommented:
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

0
Nathan RileyFounderAuthor Commented:
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.

$source = "access_token=CAAWhyYsjaWsBAOCwjZCsZC4TMZBlYwMF76RkK0XvX6QP2vuOWsxCSD6u9dTQtU2xHm70t9652qvFKAmYhkHnSQX3MOlAdOI9rvZBJTM959rWWXsTt3AZBINv6zcRS6y5kx19Wdl5J8uiJLTM2nBPW1xVwOfT4E4OARNZA9KlbhGuJwgYLASpMiqloke9DQhe4ZD&expires=5180531";
parse_str($source, $output);
    $source = $output['access_token'];

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nathan RileyFounderAuthor Commented:
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.
0
Nathan RileyFounderAuthor Commented:
Just closing this out, already stated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.