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.

https://www.youtube.com/watch?v=LlNpVFxQUtE
https://www.youtube.com/watch?v=UZiHPLGh-Ek
https://www.youtube.com/watch?v=zyC3CWDlEzY
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.

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

$fileName = 'https://www.youtube.com/watch?v=LlNpVFxQUtE';

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

Open in new window


My question is this a good solution?  Is there a better way?
0
Julian HansenCommented:
Here some more options
<?php
$fileName = 'https://www.youtube.com/watch?v=LlNpVFxQUtE';

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
0

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.
0
Ray PaseurCommented:
This works well with your sample data:
http://php.net/manual/en/function.stristr.php

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:

https://www.youtube.com/watch?embedded=true&v=LlNpVFxQUtE

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:

https://www.youtube.com/watch?v=

But because the URL arguments are not required in any given order, it might be better to parse the URL:
http://php.net/manual/en/function.parse-url.php

Once we have that done, we can parse the query string and look for the "v=" part:
http://php.net/manual/en/function.parse-str.php

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).
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags
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.