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

    Extracting a href and title out of this string

    Hi Guys,

    I want to get the href and title out of this html code below..

    PHP Code:
    <li class="images"><a href="www.something.com/firstpage/" title="title1"><span>7</span></a></li
    I set the html code as a string


    PHP Code:
    $strContent"<li class="images"><a href="www.something.com/firstpage/" title="title1"><span>7</span></a></li>"
    and set up this funtion with the regular expression below

    PHP Code:

    $strListMatches 
    '!<li class="images" href="(.*)" title="(.*)"><span>(.*)</span</a></li>!isU';


        function 
    getMatches($strMatch,$strContent
      {
            if(
    preg_match_all($strMatch,$strContent,$objMatches))
        {
                return 
    $objMatches;
            }
            return 
    "";
        } 
    i tryed this piece of code to print the title but it wont work.

    PHP Code:
     print_r($objListMatches[1]); 
    Is there something wrong with the regular expression?



    iv tryed this regular expression but it wont work

    PHP Code:
    $strListMatches '!<li class="images" href="(.*)" title="(.*)"><span>(.*)</span></a></li>!isU'

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Don't use .* unless you fully understand it. If you want to extract a quoted section, you should use ".*?" or "[^"]*"
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    #<[^>]*href=\"([^\"]*)\"[^>]*>#is 
    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
    •