abhi_madhani Posted March 28, 2011 Share Posted March 28, 2011 Hi, I am not that good with understanding REGEX/REGEXP functions but, Could someone please let me know as how can I search through html code for a particular tag having a definite start and definite end. E.g. HTML Code below, it has definite Start of <div id="ST and definite End of house='Anything'></div></div></div> <div id="ST12345678_st" class="r_right_col"> <div class=""></div> <div id=""><span class=""></span></div> <div class=""></div> <div class=""><span class=""></span></div> <div class=""></div> <div class=""> <div style="" id="" fname=Abhishek lname='madhani' division=8C house='Anything'> </div> </div> </div> So the HTML file which I have, has the this particular tag being repeated atleast more than 50 times in whole code. And I am planning to extract each of them seperately. So if someone could help me with REGEX to search for this particular tags in whole file, would be really helpful. Regards, Abhishek Madhani Quote Link to comment https://forums.phpfreaks.com/topic/231916-regex-regular-expression-for-search/ Share on other sites More sharing options...
sasa Posted March 28, 2011 Share Posted March 28, 2011 preg_match_all('/<div id="ST.*?house=\'Anything\'>\s*<\/div>\s*<\/div>\s*<\/div>/is', $html, $match); print_r($match); Quote Link to comment https://forums.phpfreaks.com/topic/231916-regex-regular-expression-for-search/#findComment-1193154 Share on other sites More sharing options...
abhi_madhani Posted March 28, 2011 Author Share Posted March 28, 2011 Hi, I checked this it doesn't seem to work, there need be included a character after "ST.* to allow to go to next line, I think. \<div id="ST.*?house=\'Anything\'>\s*<\/div>\s*<\/div>\s*<\/div>/is Also this is very good website, where one could check the outcome of the preg_match with regex. http://regex.larsolavtorvik.com/ Could someone please help me with this? Regards, Abhishek Quote Link to comment https://forums.phpfreaks.com/topic/231916-regex-regular-expression-for-search/#findComment-1193158 Share on other sites More sharing options...
sasa Posted March 28, 2011 Share Posted March 28, 2011 i try this code <?php $html = 'bla bl bla<div id="ST12345678_st" class="r_right_col"> <div class=""></div> <div id=""><span class=""></span></div> <div class=""></div> <div class=""><span class=""></span></div> <div class=""></div> <div class=""> <div style="" id="" fname=Abhishek lname=\'madhani\' division=8C house=\'Anything\'> </div> </div> </div>bla bla bla bl bla<div id="ST22345678_st" class="r_right_col"> <div class=""></div> <div id=""><span class=""></span></div> <div class=""></div> <div class=""><span class=""></span></div> <div class=""></div> <div class=""> <div style="" id="" fname=Abhishek lname=\'madhani\' division=8C house=\'Anything\'> </div> </div> </div>bla bla'; preg_match_all('/<div id="ST.*?house=\'Anything\'>\s*<\/div>\s*<\/div>\s*<\/div>/is', $html, $match); print_r($match); ?> and it output this X-Powered-By: PHP/5.2.0 Content-type: text/html Array ( [0] => Array ( [0] => <div id="ST12345678_st" class="r_right_col"> <div class=""></div> <div id=""><span class=""></span></div> <div class=""></div> <div class=""><span class=""></span></div> <div class=""></div> <div class=""> <div style="" id="" fname=Abhishek lname='madhani' division=8C house='Anything'> </div> </div> </div> [1] => <div id="ST22345678_st" class="r_right_col"> <div class=""></div> <div id=""><span class=""></span></div> <div class=""></div> <div class=""><span class=""></span></div> <div class=""></div> <div class=""> <div style="" id="" fname=Abhishek lname='madhani' division=8C house='Anything'> </div> </div> </div> ) ) Quote Link to comment https://forums.phpfreaks.com/topic/231916-regex-regular-expression-for-search/#findComment-1193173 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.