Insoftservice inso
asked on
Data between html tags using regex in php
I wanted to extract info between html tags .
for eg
<div>
	 <br>
		 love to marry is an free matrimonial website
	 </br>
	 <div id='first'>
		 Search for your partner ends here.
	 </div>
	 <div class="test">
		 love to marry will let you know what kanyadaan means
		</div>
<span>
Provide your ads on our website at very cheap rate
</span>
	<div id="second">
	 Mangal sutra bandhan is an hindu ritual where groom .. to read more please visit http://lovetomarry.com/ritual
<table>
<tr>
		<td>mangal sutra bandhan information</td>
		<td>Find your partner for free </td>
</tr>
<tr>
		<td>send your love sms at http://lovetomarry.com and win exciting prizes </td>
		<td>Get your horoscope built from love to marry , to know more about your future your future enroll our website </td>
</tr>
</table>
	 <div>
	 Watch latest online movies for free
	 </div>
	
	 Get your daily, weekly, monthly,yearly	Horoscop e for free only on lovetomarry.com
	</div>
</div>
########### o/p ##############
if request is for "BR" tags
love to marry is an free matrimonial website
if request is for "DIV" tags w.r.t id "first"
Search for your partner ends here.
if request is for "DIV" tags w.r.t class "test"
love to marry will let you know what kanyadaan means.
if request is for "SPAN"
Provide your ads on our website at very cheap rate
if request is for "DIV" tags w.r.t id "Second"
	 Mangal sutra bandhan is an hindu ritual where groom .. to read more please visit http://lovetomarry.com/ritual
	 mangal sutra bandhan information
	 Find your partner for free
	 send your love sms at http://lovetomarry.com and win exciting prizes
	 Get your horoscope built from love to marry , to know more about your future your future enroll our website
	
	 Watch latest online movies for free
	 Get your daily, weekly, monthly,yearly	Horoscop e for free only on lovetomarry.com
###################o/p#### ########## ########## ########&# 9;
If request is for "DIV" tags w.r.t id "Second"
over here i want to exclude table but if it contains some other html tags then i have to maintain the text with there respective tags.
extraction of data within the html tags may be with "id","class" or without there attributes.
i have extracted data between tags w.r.t id but it fails if it has some other div in between
for eg
<div>
	 <br>
		 love to marry is an free matrimonial website
	 </br>
	 <div id='first'>
		 Search for your partner ends here.
	 </div>
	 <div class="test">
		 love to marry will let you know what kanyadaan means
		</div>
<span>
Provide your ads on our website at very cheap rate
</span>
	<div id="second">
	 Mangal sutra bandhan is an hindu ritual where groom .. to read more please visit http://lovetomarry.com/ritual
<table>
<tr>
		<td>mangal sutra bandhan information</td>
		<td>Find your partner for free </td>
</tr>
<tr>
		<td>send your love sms at http://lovetomarry.com and win exciting prizes </td>
		<td>Get your horoscope built from love to marry , to know more about your future your future enroll our website </td>
</tr>
</table>
	 <div>
	 Watch latest online movies for free
	 </div>
	
	 Get your daily, weekly, monthly,yearly	Horoscop
	</div>
</div>
########### o/p ##############
if request is for "BR" tags
love to marry is an free matrimonial website
if request is for "DIV" tags w.r.t id "first"
Search for your partner ends here.
if request is for "DIV" tags w.r.t class "test"
love to marry will let you know what kanyadaan means.
if request is for "SPAN"
Provide your ads on our website at very cheap rate
if request is for "DIV" tags w.r.t id "Second"
	 Mangal sutra bandhan is an hindu ritual where groom .. to read more please visit http://lovetomarry.com/ritual
	 mangal sutra bandhan information
	 Find your partner for free
	 send your love sms at http://lovetomarry.com and win exciting prizes
	 Get your horoscope built from love to marry , to know more about your future your future enroll our website
	
	 Watch latest online movies for free
	 Get your daily, weekly, monthly,yearly	Horoscop
###################o/p####
If request is for "DIV" tags w.r.t id "Second"
over here i want to exclude table but if it contains some other html tags then i have to maintain the text with there respective tags.
extraction of data within the html tags may be with "id","class" or without there attributes.
i have extracted data between tags w.r.t id but it fails if it has some other div in between
ASKER
thx for info HTML parser can also be used.
as you have mention does it will provide right info for id second as it contains div inside div.
Even i am trying for html parser for this issue
Tried but have got this issue.
syntax error, unexpected '^', expecting T_STRING or T_VARIABLE or T_NUM_STRING
as you have mention does it will provide right info for id second as it contains div inside div.
Even i am trying for html parser for this issue
Tried but have got this issue.
syntax error, unexpected '^', expecting T_STRING or T_VARIABLE or T_NUM_STRING
Give me a little while to test. I'm not currently near a machine with PHP installed, but I'll have one shortly. I'll post back with a correction.
ASKER
not an issue. take your own time .
Enjoy your new year that's more important.
Happy new year to you & your family and to all EE family
Enjoy your new year that's more important.
Happy new year to you & your family and to all EE family
OK, let's try it like this:
$tag = 'br';
$attr = '';
// OR
//$tag = 'div';
//$attr = 'class="test"';
// ******************* //
$tag = preg_quote($tag);
$attr = preg_quote($attr);
$pattern = "/<" . $tag . "(?: +[^>]*" . $attr . "[^>]*)?>((?:<|[^<](?!\/" . $tag . ">))+)<\/" . $tag . ">/";
if (preg_match($pattern, $html, $matches)) {
echo $matches[1];
}
ASKER
not working dude. its showing blank . html parser not working its showing   entity.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Html has other tags too i have just provided the example of the div.But it could contain <br> and other html tags too.like <p>@ and so on.
When tried with the code provided it gave me blank result , but when tried with html parser as you suggested it gave me entity error.
When tried with the code provided it gave me blank result , but when tried with html parser as you suggested it gave me entity error.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx @fibo & @aarontomosky i will surely try this .
Hi,
What are the results?
What are the results?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx
Open in new window