...

View Full Version : REGEX Pattern - match only if it doesn't starts with <script type="text/javascript">



BornKillaz
11-08-2012, 12:14 AM
Hello all!

I'm posting this at the PHP section, because I'm working it with a php function.

So I need to grab the text between these HTML tags:

Votes )</small></span> </p>
The text I need, will appear in here. <p>
<div class="extravote-container-small">

So, I end up with something like this:


<?php

preg_match_all( '/Votes\s\)<\/small><\/span>\s<\/p>(.*?)\s<p>\s*<div\sclass="extravote-container-small">/si' , $page, $arr );

print_r( $arr[1]);

<?php


And that works just fine for strings similar to the above example. However there will be some strings that will contain a little JavaScript snippet (Google Adsense) right before the text I want, so what I need in addiction for the REGEX I mentioned above, is that when it finds something like this...


Votes )</small></span> </p>

<script type="text/javascript"><!--
google_ad_client = "pub-0000000000000000";
/* 728x90, created 00/00/00 */
google_ad_slot = "000000000";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

And this is another text I need. <p>
<div class="extravote-container-small">


...it must not match that JavaScript section in the results.

Can someone please help me out on this?

I know I could use strip_tags() after getting the result all together, but I was avoiding it.

Thank you very much.

poyzn
11-08-2012, 06:10 AM
$text = 'Votes )</small></span> </p> <script type="text/javascript">
<!-- google_ad_client = "p$ub-0000000000000000"; /* 728x90,.
created 00/00/00 */ google_ad_slot = "000000000";.
google_ad_width = 728; google_ad_height = 90; //-->.
</script>.
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>.
And this is another text I need. <p>.
<div class="extravote-container-small">';

if(preg_match('/<script/', $text)) {
preg_match('/Votes.+<\/script>([^<]+)/s', $text, $string);
echo $string[1];
}

BornKillaz
11-08-2012, 03:29 PM
Thank you very much poyzn!

I'll give it a try and let you know how it work. I knew it had to do something with look ahead, but I don't know much about those yet. Do you know any good tutorial?

Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum