Regular Expression Help

Discussion in 'PHP' started by PHPGator, Jul 29, 2009.

  1. #1
    I'm not going to lie, i'm not very good with regular expressions and I'm sure someone here is that could quickly help me.

    I have some HTML and would like to pull few specific things out of it and then throw it into a MySQL database. I can't figure out a regular expression that would do this.

    Here is one of the many listings in my HTML code:

    <div class="ListingResult Sponsored">
    	<div class="packageTag">Sponsored</div>
    	<h2 class="white"><a href="/listing/search-and-content-ad-server-for-vertical-markets/">The Ad Network Platform Built for the Enterprise - Free Edition*</a></h2>
    
    	<div class="listingSponsoredImage"><img src="x" alt="" /></div>
    	<div class="listingDescriptionNarrow">
    		<p>
    			Start your own advertising network with the right mix of features and capabilities. Featuring our exclusive Yield Optimization Engine, advertisers and publishers can count on you for delivering ads that are not only highly targeted, but also provide the<span id="moreInfo_69878" class="moreInfoBody" style="display:none;"> highest revenue. Out-of-the-box, or as the foundation for your project, we provide features that matter: Pluggable Framework, auction-based bidding, robust reporting tools, geographical targeting, region targeting, custom filters, a simple self-service interface, integrated payment system support, developers API, contextual ad matching, exclusive proactive click-fraud filtering, native scalability support, extensive system security, and so much more. Use our pluggable framework and API to extend the platform to include your own custom features and capabilities. Try the inClick Ad Server on us today. Have questions? Give us a call at (800)863-1943, +1(248)616-1943, or Skype: inmotiongroup. We're in the USA.</span><span class="showLink" id="showMoreLink_69878">...<br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showMoreLink_69878&quot;); Element.show(&quot;showLessLink_69878&quot;); return false;">show more</a>)</span><span class="showLink" id="showLessLink_69878" style="display:none;"><br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showLessLink_69878&quot;); Element.show(&quot;showMoreLink_69878&quot;); return false;">show less</a>)</span>		</p>
    	</div>
    
    			<div class="jumpLinks">
    			<ul>
    				<li><a href="/listing/search-and-content-ad-server-for-vertical-markets/">More Details</a></li><li><a href="/listings/jump/site/69878/clp/" rel="nofollow" target="_blank">Visit Publisher Site</a></li><li><a href="/listings/jump/download/69878/clp/" rel="nofollow" target="_blank">Download</a></li>			</ul>
    		</div>
    	
    	
    			
    
    	<div class="listingFooter">
    		<div class="listingFooterReviews">
    			<p class="rating rating4-5">
    			(4.74 out of 5) from <a href="/listing/search-and-content-ad-server-for-vertical-markets/" rel="nofollow">17 Ratings</a>			</p>
    		</div>
    		<ul class="listingFooterRight">
    			<li><strong>Price:</strong> Commercial</li>
    			<li><strong>Views:</strong> 15,116</li>
    		</ul>
    		<div class="clearAll"><!-- Clears all floats in footer --></div>
    	</div>
    
    </div>
    Code (markup):
    I would like to see it set this as a variable or place it in an array since there will be multiple listings I want the script to go through (Consider the code to be double the length if necessary).

    I want it to pull from the above code:

    
    1: The Ad Network Platform Built for the Enterprise - Free Edition*
    2: Commercial
    3: 15,116
    
    Code (markup):
    These variables would stand for (respectively):
    The Name
    The Price
    The Views
    Code (markup):
    If you could help me with the few lines of code I would be greatly appreciative!
     
    PHPGator, Jul 29, 2009 IP
  2. Leron

    Leron Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    53
    #2
    Not sure what you ment by the lie part :rolleyes: but
    Here is my solution:
    
    $mystring= <<<ADPLUS
    <div class="ListingResult Sponsored">
    	<div class="packageTag">Sponsored</div>
    	<h2 class="white"><a href="/listing/search-and-content-ad-server-for-vertical-markets/">The Ad Network Platform Built for the Enterprise - Free Edition*</a></h2>
    
    	<div class="listingSponsoredImage"><img src="x" alt="" /></div>
    	<div class="listingDescriptionNarrow">
    		<p>
    			Start your own advertising network with the right mix of features and capabilities. Featuring our exclusive Yield Optimization Engine, advertisers and publishers can count on you for delivering ads that are not only highly targeted, but also provide the<span id="moreInfo_69878" class="moreInfoBody" style="display:none;"> highest revenue. Out-of-the-box, or as the foundation for your project, we provide features that matter: Pluggable Framework, auction-based bidding, robust reporting tools, geographical targeting, region targeting, custom filters, a simple self-service interface, integrated payment system support, developers API, contextual ad matching, exclusive proactive click-fraud filtering, native scalability support, extensive system security, and so much more. Use our pluggable framework and API to extend the platform to include your own custom features and capabilities. Try the inClick Ad Server on us today. Have questions? Give us a call at (800)863-1943, +1(248)616-1943, or Skype: inmotiongroup. We're in the USA.</span><span class="showLink" id="showMoreLink_69878">...<br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showMoreLink_69878&quot;); Element.show(&quot;showLessLink_69878&quot;); return false;">show more</a>)</span><span class="showLink" id="showLessLink_69878" style="display:none;"><br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showLessLink_69878&quot;); Element.show(&quot;showMoreLink_69878&quot;); return false;">show less</a>)</span>		</p>
    	</div>
    
    			<div class="jumpLinks">
    			<ul>
    				<li><a href="/listing/search-and-content-ad-server-for-vertical-markets/">More Details</a></li><li><a href="/listings/jump/site/69878/clp/" rel="nofollow" target="_blank">Visit Publisher Site</a></li><li><a href="/listings/jump/download/69878/clp/" rel="nofollow" target="_blank">Download</a></li>			</ul>
    		</div>
    	
    	
    			
    
    	<div class="listingFooter">
    		<div class="listingFooterReviews">
    			<p class="rating rating4-5">
    			(4.74 out of 5) from <a href="/listing/search-and-content-ad-server-for-vertical-markets/" rel="nofollow">17 Ratings</a>			</p>
    		</div>
    		<ul class="listingFooterRight">
    			<li><strong>Price:</strong> Commercial</li>
    			<li><strong>Views:</strong> 15,116</li>			
    		</ul>
    		<div class="clearAll"><!-- Clears all floats in footer --></div>
    	</div>
    
    </div>
    ADPLUS;
    
    PHP:
    
    $mystring=strtr($mystring, array("\n" => "", "\r\n" =>"","\t"=>""));
    preg_match('/<h2[^>]*?><a[^>]*?>(.*?)<\/a><\/h2>.+<ul class="listingFooterRight">(.*?)<\/ul>/i',$mystring,$holdit);
    preg_match_all('/<\/strong>\s*(.*?)\s*<\/li>/i',$holdit[2],$holdit2);
    
    print "<pre>".$holdit[1];
    print_r($holdit2[1]);
    
    PHP:
    Loop $holdit and $holdit2 where needed.

    Let me know what you think :D
     
    Leron, Jul 29, 2009 IP
  3. PHPGator

    PHPGator Banned

    Messages:
    4,437
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    260
    #3
    Leron, I sent you a PM as well because I know it took me a while to respond. Your code works flawlessly! Thank you for helping me out there. Unfortunately though, I can't seem to get it to loop. Do you think you could help me out here too (or anyone else)?

    Thanks!
     
    PHPGator, Jul 30, 2009 IP
  4. Leron

    Leron Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    53
    #4
    Glad it worked out for you. :D

    Here is a better test, you may have to modify it based on actual html file you are looking at:
    
    $mystring= <<<ADPLUS
    <div class="ListingResult Sponsored">
    	<div class="packageTag">Sponsored</div>
    	<h2 class="white"><a href="/listing/search-and-content-ad-server-for-vertical-markets/">The Ad Network Platform Built for the Enterprise - Free Edition*</a></h2>
    
    	<div class="listingSponsoredImage"><img src="x" alt="" /></div>
    	<div class="listingDescriptionNarrow">
    		<p>
    			Start your own advertising network with the right mix of features and capabilities. Featuring our exclusive Yield Optimization Engine, advertisers and publishers can count on you for delivering ads that are not only highly targeted, but also provide the<span id="moreInfo_69878" class="moreInfoBody" style="display:none;"> highest revenue. Out-of-the-box, or as the foundation for your project, we provide features that matter: Pluggable Framework, auction-based bidding, robust reporting tools, geographical targeting, region targeting, custom filters, a simple self-service interface, integrated payment system support, developers API, contextual ad matching, exclusive proactive click-fraud filtering, native scalability support, extensive system security, and so much more. Use our pluggable framework and API to extend the platform to include your own custom features and capabilities. Try the inClick Ad Server on us today. Have questions? Give us a call at (800)863-1943, +1(248)616-1943, or Skype: inmotiongroup. We're in the USA.</span><span class="showLink" id="showMoreLink_69878">...<br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showMoreLink_69878&quot;); Element.show(&quot;showLessLink_69878&quot;); return false;">show more</a>)</span><span class="showLink" id="showLessLink_69878" style="display:none;"><br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showLessLink_69878&quot;); Element.show(&quot;showMoreLink_69878&quot;); return false;">show less</a>)</span>		</p>
    	</div>
    
    			<div class="jumpLinks">
    			<ul>
    				<li><a href="/listing/search-and-content-ad-server-for-vertical-markets/">More Details</a></li><li><a href="/listings/jump/site/69878/clp/" rel="nofollow" target="_blank">Visit Publisher Site</a></li><li><a href="/listings/jump/download/69878/clp/" rel="nofollow" target="_blank">Download</a></li>			</ul>
    		</div>
    	
    	
    			
    
    	<div class="listingFooter">
    		<div class="listingFooterReviews">
    			<p class="rating rating4-5">
    			(4.74 out of 5) from <a href="/listing/search-and-content-ad-server-for-vertical-markets/" rel="nofollow">17 Ratings</a>			</p>
    		</div>
    		<ul class="listingFooterRight">
    			<li><strong>Price:</strong> Commercial</li>
    			<li><strong>Views:</strong> 15,116</li>			
    		</ul>
    		<div class="clearAll"><!-- Clears all floats in footer --></div>
    	</div>
    
    </div>
    
    
    
    <div class="ListingResult Sponsored">
    	<div class="packageTag">Sponsored</div>
    	<h2 class="white"><a href="/listing/search-and-content-ad-server-for-vertical-markets/">The Ad Network Platform Built for the Enterprise - Free Edition*</a></h2>
    
    	<div class="listingSponsoredImage"><img src="x" alt="" /></div>
    	<div class="listingDescriptionNarrow">
    		<p>
    			Start your own advertising network with the right mix of features and capabilities. Featuring our exclusive Yield Optimization Engine, advertisers and publishers can count on you for delivering ads that are not only highly targeted, but also provide the<span id="moreInfo_69878" class="moreInfoBody" style="display:none;"> highest revenue. Out-of-the-box, or as the foundation for your project, we provide features that matter: Pluggable Framework, auction-based bidding, robust reporting tools, geographical targeting, region targeting, custom filters, a simple self-service interface, integrated payment system support, developers API, contextual ad matching, exclusive proactive click-fraud filtering, native scalability support, extensive system security, and so much more. Use our pluggable framework and API to extend the platform to include your own custom features and capabilities. Try the inClick Ad Server on us today. Have questions? Give us a call at (800)863-1943, +1(248)616-1943, or Skype: inmotiongroup. We're in the USA.</span><span class="showLink" id="showMoreLink_69878">...<br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showMoreLink_69878&quot;); Element.show(&quot;showLessLink_69878&quot;); return false;">show more</a>)</span><span class="showLink" id="showLessLink_69878" style="display:none;"><br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showLessLink_69878&quot;); Element.show(&quot;showMoreLink_69878&quot;); return false;">show less</a>)</span>		</p>
    	</div>
    
    			<div class="jumpLinks">
    			<ul>
    				<li><a href="/listing/search-and-content-ad-server-for-vertical-markets/">More Details</a></li><li><a href="/listings/jump/site/69878/clp/" rel="nofollow" target="_blank">Visit Publisher Site</a></li><li><a href="/listings/jump/download/69878/clp/" rel="nofollow" target="_blank">Download</a></li>			</ul>
    		</div>
    	
    	
    			
    
    	<div class="listingFooter">
    		<div class="listingFooterReviews">
    			<p class="rating rating4-5">
    			(4.74 out of 5) from <a href="/listing/search-and-content-ad-server-for-vertical-markets/" rel="nofollow">17 Ratings</a>			</p>
    		</div>
    		<ul class="listingFooterRight">
    			<li><strong>Price:</strong> Commercial</li>
    			<li><strong>Views:</strong> 15,116</li>			
    		</ul>
    		<div class="clearAll"><!-- Clears all floats in footer --></div>
    	</div>
    
    </div>
    
    
    <div class="ListingResult Sponsored">
    	<div class="packageTag">Sponsored</div>
    	<h2 class="white"><a href="/listing/search-and-content-ad-server-for-vertical-markets/">The Ad Network Platform Built for the Enterprise - Free Edition*</a></h2>
    
    	<div class="listingSponsoredImage"><img src="x" alt="" /></div>
    	<div class="listingDescriptionNarrow">
    		<p>
    			Start your own advertising network with the right mix of features and capabilities. Featuring our exclusive Yield Optimization Engine, advertisers and publishers can count on you for delivering ads that are not only highly targeted, but also provide the<span id="moreInfo_69878" class="moreInfoBody" style="display:none;"> highest revenue. Out-of-the-box, or as the foundation for your project, we provide features that matter: Pluggable Framework, auction-based bidding, robust reporting tools, geographical targeting, region targeting, custom filters, a simple self-service interface, integrated payment system support, developers API, contextual ad matching, exclusive proactive click-fraud filtering, native scalability support, extensive system security, and so much more. Use our pluggable framework and API to extend the platform to include your own custom features and capabilities. Try the inClick Ad Server on us today. Have questions? Give us a call at (800)863-1943, +1(248)616-1943, or Skype: inmotiongroup. We're in the USA.</span><span class="showLink" id="showMoreLink_69878">...<br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showMoreLink_69878&quot;); Element.show(&quot;showLessLink_69878&quot;); return false;">show more</a>)</span><span class="showLink" id="showLessLink_69878" style="display:none;"><br />(<a href="#/" rel="nofollow" onclick="Effect.toggle(&quot;moreInfo_69878&quot;, &quot;blind&quot;, {duration: 0.2}); Element.hide(&quot;showLessLink_69878&quot;); Element.show(&quot;showMoreLink_69878&quot;); return false;">show less</a>)</span>		</p>
    	</div>
    
    			<div class="jumpLinks">
    			<ul>
    				<li><a href="/listing/search-and-content-ad-server-for-vertical-markets/">More Details</a></li><li><a href="/listings/jump/site/69878/clp/" rel="nofollow" target="_blank">Visit Publisher Site</a></li><li><a href="/listings/jump/download/69878/clp/" rel="nofollow" target="_blank">Download</a></li>			</ul>
    		</div>
    	
    	
    			
    
    	<div class="listingFooter">
    		<div class="listingFooterReviews">
    			<p class="rating rating4-5">
    			(4.74 out of 5) from <a href="/listing/search-and-content-ad-server-for-vertical-markets/" rel="nofollow">17 Ratings</a>			</p>
    		</div>
    		<ul class="listingFooterRight">
    			<li><strong>Price:</strong> Commercial</li>
    			<li><strong>Views:</strong> 15,116</li>			
    		</ul>
    		<div class="clearAll"><!-- Clears all floats in footer --></div>
    	</div>
    
    </div>
    ADPLUS;
    
    PHP:
    Ok I modified it a bit. Still needs some work. But you should be able to get going from here:
    
    $mystring=strtr($mystring, array("\n" => "", "\r\n" =>"","\t"=>""));
    $newstring=preg_split('/<(\w+) class="ListingResult Sponsored">/i',$mystring);
    
    foreach($newstring as $value){
    if(!$value) continue;
    	preg_match('/<h2[^>]*?><a[^>]*?>(.*?)<\/a><\/h2>.+<ul class="listingFooterRight">(.*?)<\/ul>/i',$value,$holdit);
    	preg_match_all('/<\/strong>\s*(.*?)\s*<\/li>/i',$holdit[2],$holdit2);
    
    
    print "Name:$holdit[1]<br>".
    "Price: {$holdit2[1][0]}<br>".
    "Views{$holdit2[1][1]}<br><Br>";
    }
    
    PHP:
     
    Leron, Jul 30, 2009 IP