Solved

Problem with PHP preg_match_all

Posted on 2009-04-10
5
250 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete image(s) associated with record(s) 16 46
PHP processing webform 25 45
How can I send attachment with mail 5 26
How to convert PHP array into string ? 16 28
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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 look for a specific file type in a local or remote server directory using PHP.

710 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