Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2012
    Location
    Switzerland
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post

    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.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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];


  • Users who have thanked poyzn for this post:

    BornKillaz (11-08-2012)

  • #3
    New Coder
    Join Date
    Jun 2012
    Location
    Switzerland
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    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!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •