PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Re: [PHP] Little regex please...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: [PHP] Little regex please...

Réponse
 
LinkBack Outils de la discussion
Vieux 13/10/2008, 19h09   #1
Ryan S
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Little regex 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




  Réponse avec citation
Vieux 13/10/2008, 19h12   #2
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Little regex please...

Ryan S wrote:
> 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
>
>
>
>
>


Take off the first /

preg_match('#<title>([^<]*)<\/title>#iU',$data,$match);
--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

  Réponse avec citation
Vieux 13/10/2008, 19h13   #3
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Little regex please...

Ryan S wrote:
> 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
>
>
>
>
>


Too quick on the reply....

forgot not to escape the forward slash on the closing title tag

preg_match('#<title>([^<]*)</title>#iU',$data,$match);

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

  Réponse avec citation
Vieux 13/10/2008, 19h34   #4
Leon du Plessis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] Little regex please...


-----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 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

  Réponse avec citation
Vieux 13/10/2008, 20h18   #5
Boyd, Todd M.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] Little regex please...

> -----Original Message-----
> From: Jim Lucas [mailto:lists@cmsws.com]
> Sent: Monday, October 13, 2008 12:13 PM
> To: Ryan S
> Cc: Eric Butera; Boyd, Todd M.; php php
> Subject: Re: [php] Little regex please...
>
> Too quick on the reply....
>
> forgot not to escape the forward slash on the closing title tag
>
> preg_match('#<title>([^<]*)</title>#iU',$data,$match);


That'll do, Jim.

I also thought it worth mentioning that by telling a catch-all character
sequence (.*) not to be "greedy", another viable solution would be this:

preg_match('#<title>(.*?)</title>#iU', $data, $match);

The ? will stop it when it reaches </title> through back-tracking.
Probably less-efficient in more complex regex patterns, but I find it
can make the patterns more readable at-a-glance in regex-heavy code
blocks.

Sorry.. a bit OT.. and the OP has already been answered. Meh.


Todd Boyd
Web Programmer



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12010 seconds with 13 queries