Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with PHP preg_match_all

Posted on 2009-04-10
5
Medium Priority
?
270 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
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…
Suggested Courses

916 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