Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with PHP preg_match_all

Posted on 2009-04-10
5
Medium Priority
?
262 Views
Last Modified: 2012-05-06
I'm tryign to crap a web site but I think an ascii character code is messing it up.

Everythign works fine untill I try to get the title of a video..   I load the page into $html, in the beginnign section of my script, I need to get the video code so I have already set the $item.

The complete line in the html is "<h1><a href="/watch/265126/come-on-in-im-home-pt-13/"> come on in, i&#39;m home,  pt 1/3</a></h1>"   I want to extract the title of the video between ""<h1><a href="/watch/265126/come-on-in-im-home-pt-13/">" and "</a></h1>"

The problem is, I could extract the title as is, but the html is always changing, my variable $item will always be updated with the nex code's need.  I think the &#39; is giving me a problem on that one video which breaks my script.

$item = "<h1><a href="/watch/265126/come-on-in-im-home-pt-13/">";
preg_match_all("/".$item."(.+?)\<\/a><\/h1>/is", $html, $match );
 
 
Debug Warning: D:\www\youtube1.php line 125 - preg_match_all() [<a href='function.preg-match-all'>function.preg-match-all</a>]: Unknown modifier 'w'

Open in new window

0
Comment
Question by:megarry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 18

Expert Comment

by:Hube02
ID: 24115801
The problem that is generating the error has to do with the fact that there are /s in $item that are not escaped. You are using the / as a delimiter and preg_match all is seeing the / in "/watch/265126/come" as the closing delimiter and this is why it says "Unknown modifier 'w'"

try the following (note that I removed your escaping of /s and changed the delimiter to a #

$item = '<h1><a href="/watch/265126/come-on-in-im-home-pt-13/">';
preg_match_all("#".$item."(.+?)\</a></h1>#is", $html, $match );

Open in new window

0
 
LVL 18

Expert Comment

by:Hube02
ID: 24115821
another minor change, the < should not need to be escaped

$item = "<h1><a href="/watch/265126/come-on-in-im-home-pt-13/">";
preg_match_all("/".$item."(.+?)<\/a><\/h1>/is", $html, $match );

Open in new window

0
 
LVL 18

Accepted Solution

by:
Hube02 earned 2000 total points
ID: 24115828
Sorry, coppied the wrong code

$item = '<h1><a href="/watch/265126/come-on-in-im-home-pt-13/">';
preg_match_all("#".$item."(.+?)</a></h1>#is", $html, $match );

Open in new window

0
 

Author Comment

by:megarry
ID: 24115831
I never knew you can use a delimiter of your choosing..  Always though you needed the "/ and /"..  Thanks! That will help not only with this problem but moving ahead!  

Works perfectly now..  :)
0
 
LVL 18

Expert Comment

by:Hube02
ID: 24116188
You can also use sets of characters, for instance < and > or [ and ], though I don't generally use these. I like to use the # when dealing with html because this character usually does not appear in the regular expression.

They don't really cover this very well in what I've found of online documentation.

An excellent resource for anyone interested in regular expressions is Mastering Regular Expressions by Jeffrey Friedl http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=sr_1_1?ie=UTF8&s=books&qid=1239376459&sr=1-1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question