Topic: PHP coding help.

Hi mighty coders,

For example I have a code below:

<a href="http://www.youtube.com/embed/yaEUrl8irZQ?rel=0">http://www.youtube.com/embed/yaEUrl8irZQ?rel=0</a>

Can anyone show me how or which PHP function should I use to extract the "http://www.youtube.com/embed/yaEUrl8irZQ?rel=0" part only?

Please help. Much much appreciated

Re: PHP coding help.

You're going to have to learn about regular expressions, but I would investigate http://www.php.net/manual/en/function.preg-match.php.

Re: PHP coding help.

Thanks, I did research and it was too complicated for me to convert a long text like this. Can you help me write a pattern? sad

4 (edited by Kushi 2012-01-19 05:46)

Re: PHP coding help.

There's a function explode
Perhaps

<?
$address = '<a href="http://www.youtube.com/embed/yaEUrl8irZQ?rel=0">http://www.youtube.com/embed/yaEUrl8irZQ?rel=0</a>';
echo $address.'</br>';

$address= explode('"', $address);
//$address[1]; is what i need
?>

..may work tongue

If any of my dropbox link fails with 404 error, change dl.dropbox.com/u/56038890/punbb/*.zip in address to 82283017.

Currently working on rPlus - responsive theme for developers to create their own themes basing on this one.

5

Re: PHP coding help.

Parsing HTML with regular expressions is widely considered to be a bad idea.

Your best bet is to use php's in built HTML parser: http://docs.php.net/manual/en/domdocument.loadhtml.php

Re: PHP coding help.

ams wrote:

Parsing HTML with regular expressions is widely considered to be a bad idea.

Your best bet is to use php's in built HTML parser: http://docs.php.net/manual/en/domdocument.loadhtml.php

Check out the second response under the one you linked- specifically

While it is true that asking regexes to parse arbitrary HTML is like asking Paris Hilton to write an operating system, it's sometimes appropriate to parse a limited, known set of HTML.

I definitely agree that you should probably use the html parser (I didn't know it existed actually, thanks!), I don't think that using a regular expression to strip the body out of an anchor tag is an such a bad thing. The post you linked seems to be talking about replicating the functionality of a parser with regular expressions, what (I think) hoang83vu wants to do is much less complex then that.

Either way, thanks for linking that post, it was a good read!

Re: PHP coding help.

Thank you all for your supports. I have found a solution and got it working. In my case at least, parsing HTML with regular expressions was needed. I knew the patterns and just wanted to do it that way. Overall, it was a good experience using it. The extension I was working on is Fancybox2. I tried to get the image tag and video tag work with the Fancybox. Check out the result page