Solved

Problem with PHP preg_match_all

Posted on 2009-04-10
5
246 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
  • 4
5 Comments
 
LVL 18

Expert Comment

by:Hube02
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now