RE: [PHP] Little regex help please...
| From: | Leon du Plessis | Date: | Mon, 13 Oct 2008 17:34:57 +0000 |
| Subject: | RE: [PHP] Little regex help please... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-281831@lists.php.net to get a copy of this message | ||
-----Original Message-----
From: Ryan S [mailto:genphp@yahoo.com]
Sent: 13 October 2008 07:09 PM
To: Eric Butera; Boyd, Todd M.
Cc: php php
Subject: Re: [PHP] Little regex help please...
Hey Todd, Eric,
Thanks for replying.
> I don't believe you need both the / and the # for delimiters in your
> RegEx. Try using just # (since / is actually going to be in the text
> you're searching for) like this:
>
> <?php
> $data =
>
> file_get_contents("http://www.youtube.com/watch?v=oQ2dKXGAjNg");
> preg_match('#<title>([^<]*)</title>#iU', $data, $match);
> $title = $match[1];
> echo $title;
> ?>
>
>
> You can also escape the / like \/.
Ok, I changed it to:
<?php
$data = file_get_contents("http://www.youtube.com/watch?v=oQ2dKXGAjNg");
preg_match('/#<title>([^<]*)<\/title>#iU',$data,$match);
$title=$match[1];
echo $title;
?>
And this is the error i am getting:
Warning: preg_match() [function.preg-match]: No ending delimiter '/' found
in C:\wamp\www\ezee\tests\get_remote_title.php on line 3
>> Sorry forgot to include the list in my post to you
Here is another solution if you don't want to play around too much with
regex:
<?php
$data = file_get_contents("http://www.youtube.com/watch?v=oQ2dKXGAjNg");
$strt = strpos($data,"<title>")+7;
$end = strpos($data,"</title>");
$len = $end - $strt;
$title= substr($data,$strt,$len);
echo $title
?>
Produces:
"YouTube - REALLY funny ad"
As mentioned, you can probably "neaten" this up a bit....regex may be too
complex for the simple requirement needed ?
But for fun, it will be nice to see get if preg_match can do it at some
stage :-)
Regards,
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php