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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    Help with this small php script

    Please have a look at this script and tell me what is wrong..

    Im trying to extract the link and whats between <a href> </a> and i only want to take the ones which have a class of class="vehicle"

    PHP Code:

    <?php

    function getMatches($strMatch,$strContent
      {
            if(
    preg_match_all($strMatch,$strContent,$objMatches))
        {
                return 
    $objMatches;
            }
            return 
    "";
        }


      
    $strContent '<li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle1</a>
                     <li class="bus"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                     <li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle2</a>
                     <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                     <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>'
    ;

      
    $strMatch "#<li class=\"vehicle\"><a href=\"(.*)\">(.*)</a>#";
      
    $objListMatches getMatches($strMatch,$strContent);


        
    $sUrl $objListMatches[1];
        
    $sTitle $objListMatches[2];


    echo 
    $sUrl;
    echo 
    "<br />";
    echo 
    $sTitle;
    Any help will be greatly appreciated

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Stop using .* - really, just stop using it.

    '#<li class="vehicle"><a href="([^"]*)">(.*?)</a>#'

    And what is peoples obsession with double quotes?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Its good practice thats why. What if you didn't know whether it was going to be ' or "? One would have to be escaped either way. and by choice i would go with ">([^<]+)<"
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by ComputerX View Post
    Stop using .* - really, just stop using it.

    '#<li class="vehicle"><a href="([^"]*)">(.*?)</a>#'

    And what is peoples obsession with double quotes?
    Double Quote ok without using them tell me how you echo in the middle of a statment a new line or tab
    \n -- linux new line
    \r -- Mac new Line
    \r\n -- windows New line
    \t -- Tab space

    They all have to be inside "here", and allso
    PHP Code:
    $hello "hello ComputerX"
    echo "$hello\r\n";
    // outputs hello ComputerX
    //

    $hello "hello ComputerX"
    echo '$hello\r\n';
    // outputs $hello\r\n 
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    That was my next comment, " for processing, ' for not processing.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #6
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ComputerX View Post
    Stop using .* - really, just stop using it.

    '#<li class="vehicle"><a href="([^"]*)">(.*?)</a>#'

    And what is peoples obsession with double quotes?
    i have no tryed this

    PHP Code:
    <?php

    function getMatches($strMatch,$strContent
      {
            if(
    preg_match_all($strMatch,$strContent,$objMatches))
        {
                return 
    $objMatches;
            }
            return 
    "";
        }


      
    $strContent '<li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle1</a>
                     <li class="bus"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                     <li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle2</a>
                     <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                     <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>'
    ;

      
    $strMatch '#<li class="vehicle"><a href="([^"]*)">(.*?)</a>#';
      
    $objListMatches getMatches($strMatch,$strContent);


        
    $sUrl $objListMatches[1];
        
    $sTitle $objListMatches[2];


    echo 
    $sUrl;
    echo 
    "<br />";
    echo 
    $sTitle;
     

    ?>
    but am getting

    Parse error: parse error in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\carzoneCrawler\preg_match.php on line 19

    The line that the regular expression is on

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    $strMatch '#<l

    should be

    $strMatch = '#<l
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    kevinkhan (10-29-2009)

  • #8
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Thanks ha ha..

    could have spent all day looking at that and wounldnt have spotted it...

    ok so when i change

    PHP Code:
    $strContent '<li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle1</a>
                     <li class="bus"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                     <li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle2</a>
                     <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                     <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>'


    to

    PHP Code:
    $strContent file_get_contents('http://www.carzone.ie/search/results?searchsource=browse&cacheBuster=1256829754944212'); 
    it returns nothing

    Does the regular expression change for this..

    i only want to extract the titles which have the following class

    PHP Code:
    #<li class="vehicle"><a href="([^"]*)">([^<]*)</a>#is 

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    function getMatches($strMatch$strContent
    {
        if(
    preg_match_all("#".$strMatch."#is"$strContent$objMatches))
        {
            return 
    $objMatches[0];
            
    //return $objMatches[1]; for just title
        
    }
        else
        {
            return 
    false;    
        }
    }

    $strContent '<li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle1</a>
                 <li class="bus"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                 <li class="vehicle"><a href="http://www.domain.ie/abc?/">vehicle2</a>
                 <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>
                 <li class="van"><a href="http://www.domain.ie/abc?/">Alfa Romeo 147</a>'
    ;

    $strMatch '<li class="vehicle"><a href="([^"]*)">([^>]+)</a>';

    if(
    $objListMatches getMatches($strMatch$strContent))
    {
        foreach(
    $objListMatches as $vehicle)
        {
            echo 
    $vehicle."<br />";    
        }
    }
    else
    {
        
    //could add something if doesnot find anything    
        
    echo "nothing found";
    }
     
    ?>
    Last edited by Phil Jackson; 10-29-2009 at 05:52 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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