Strip Everything Before Character PHP

What's the best method to strip everything before the video unique identifier?  I want to start at the "=" and strip it and everything before in PHP.
LVL 12
Nathan RileyFounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Nathan RileyFounderAuthor Commented:
I've accomplished it with the following:

$fileName = '';

echo $uniqueIdentifier = ltrim(stristr($fileName, '='), '=');

Open in new window

My question is this a good solution?  Is there a better way?
Julian HansenCommented:
Here some more options
$fileName = '';

echo substr($fileName, 32) . "<br/>";
$result = preg_match('/^.*=(.*)$/', $fileName, $matches);
echo $matches[1] . "<br/>";
$result = explode('=',$fileName);
echo $result[1] . "<br/>";

Open in new window

Which one to use - depends on implementation and input data.

Are you always going to use the same sort of URL's - i.e. YouTube if not then eliminate the substr option - unless you combine with a strstr - similar;

preg_match - use this if the pattern on the string is likely to be variable

explode - if you know what you want is always after the '=' sign

All depends on the input data.

Nothing wrong with your implementation - if it works for you and the input dat

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
Julian HansenCommented:
If it were me I would probably use the explode or preg_match - but that is because I could envisage maybe needing the URL as well.
Ray PaseurCommented:
This works well with your sample data:

So to the instant question, "Is this a good solution," the answer is "yes."  Whether there might be a better way would depend on whether the test data posted here is truly representative of the expected input.  Stristr() might have problems getting you exactly what you want if you have a URL like this in the input:

It's usually wise to filter input and it's rare that a single PHP function will do all the things you want.  Most of the time I don't even try to reduce the function count - just take care of the things you know must be done.  In this case we know that we want to eliminate this part of the string:

But because the URL arguments are not required in any given order, it might be better to parse the URL:

Once we have that done, we can parse the query string and look for the "v=" part:

Overall, I would say this is a more durable solution than trying to write my own parser using regex.  For a really good example of why it's considered bad to try to parse with regex, see the tongue-in-cheek response here (the one with 4,000+ upvotes).
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

From novice to tech pro — start learning today.