Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 11:14 PM   PM User | #1
BornKillaz
New Coder

 
Join Date: Jun 2012
Location: Switzerland
Posts: 22
Thanks: 7
Thanked 1 Time in 1 Post
BornKillaz is an unknown quantity at this point
Question REGEX Pattern - match only if it doesn't starts with <script type="text/javascript">

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:
Code:
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 Code:
<?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...

Code:
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.

Last edited by BornKillaz; 11-07-2012 at 11:22 PM..
BornKillaz is offline   Reply With Quote
Old 11-08-2012, 05:10 AM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
PHP Code:
$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];

__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Users who have thanked poyzn for this post:
BornKillaz (11-08-2012)
Old 11-08-2012, 02:29 PM   PM User | #3
BornKillaz
New Coder

 
Join Date: Jun 2012
Location: Switzerland
Posts: 22
Thanks: 7
Thanked 1 Time in 1 Post
BornKillaz is an unknown quantity at this point
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!
BornKillaz is offline   Reply With Quote
Reply

Bookmarks

Tags
html, php, preg_match_all, regex

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:10 PM.


Advertisement
Log in to turn off these ads.