|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
-----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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
> -----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 |
|
![]() |
| Outils de la discussion | |
|
|