darkvengance Posted November 12, 2009 Share Posted November 12, 2009 Can anyone see anything wrong with this expression? I'm trying to make it so that it just grabs all the DIVs off of a page. preg_match_all('/^(<div>).*(<a)$/',$document,$holder); Quote Link to comment https://forums.phpfreaks.com/topic/181219-solved-hmm/ Share on other sites More sharing options...
cags Posted November 12, 2009 Share Posted November 12, 2009 Then yes, there are several things wrong with your pattern. a.) you use the ^ and $ which checks start and end respectively. They are used when you wish to match a string against a pattern not when you wish to search a string for sub patterns. b.) the . doesn't match newline characters by default and it seems likely being HTML there there will be some between the various characters/tags. c.) if you are trying to grab all divs you should surely be looking for </div> at some point in your pattern. d.) why do you have <a in your pattern if you simply wish to grab the divs from the page? e.) why put capture groups around <div> and <a. Since they are fixed strings there's really no point. Other than that, your good to go Quote Link to comment https://forums.phpfreaks.com/topic/181219-solved-hmm/#findComment-956051 Share on other sites More sharing options...
darkvengance Posted November 12, 2009 Author Share Posted November 12, 2009 Oh wow...just those "few" problems huh? lol...alright thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/181219-solved-hmm/#findComment-956064 Share on other sites More sharing options...
cags Posted November 12, 2009 Share Posted November 12, 2009 Just so that I don't sound like a complete downer, here's a couple of tips (much of which can be intimated from my previous post). Scrap the ^ and the $, they aren't needed in this task. Use the 's' modifer, this will make the . match newline characters. You should probably also use the 'i' modifier to make the pattern case insensitive. Off the top of my head, here's a very basic example of achieving the task... '#<div>(.*?)</div>#is' ...but this will encounter problems with nested <div>'s. You might be better off using some sort of document model. Such as DOMDocument. Quote Link to comment https://forums.phpfreaks.com/topic/181219-solved-hmm/#findComment-956111 Share on other sites More sharing options...
.josh Posted November 12, 2009 Share Posted November 12, 2009 ...but this will encounter problems with nested <div>'s. You might be better off using some sort of document model. Such as DOMDocument. and by 'might' he means 'will'. Quote Link to comment https://forums.phpfreaks.com/topic/181219-solved-hmm/#findComment-956177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.