Jump to content

Regex - Regular Expression for search


abhi_madhani

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>
        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.