SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract a value from an unordered list

    I have an unordered list like this

    <ul>
    <li>Some text</li>
    <li>Something else here</li>
    <li>#345</li>
    </ul>

    I need to only take away the number that is in the third list item and filter everything else out. It is possible that a number could be found within the first or second list items.

    How can I do a loop that extracts my number from the third <li> item every time and places it into a variable?

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it matter where the number is or does it matter to just extract numbers from the string provided?

    If it matters where the number is, you use regexp to match all <li></li> elements (probably with preg_match_all("#<ul>([(.*)^[</ul>]]</ul>#", $str, $matches); -> note, I could be wrong here) and then you explode the matches you got by </li> delimiter.
    Then you loop trough the array you just got and you use regexp to match the digits. If digit is matched, you're done.
    I could post the entire code for this, but I'm kinda in a rush to leave home so I'm sorry for not doing it

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This should do it.
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $html '
    <ul>
      <li>Some text</li>
      <li>Something else here</li>
      <li>#345</li>
    </ul>'
    ;

    $dom = new DOMDocument;
    $dom->loadHTML($html);

    echo 
    preg_replace(
      
    '~[^0-9]~',
      
    null,
      
    $dom->getElementsByTagName('li')->item(2)->nodeValue
    );

    /*
      345
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are sure that the numbers can happen only once in the string then you can easily extract with regex though I am not regex expert.
    PHP Code:
    $string '<ul>
    <li>Some text</li>
    <li>Something else here</li>
    <li>#345</li>
    </ul>'
    ;
    preg_match_all('/([0-9]+)/S'$string$matches);
    echo 
    '<pre>'print_r($matches);die(); 
    Otherwise the domdocument library as Anthony suggested already.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah the tricky bit here is that it is possible that numbers may exist in the first or second list items. So I can't do a match for numbers on the whole list.

    Thanks to AnthonySterling for this bit:

    PHP Code:
    $dom->getElementsByTagName('li')->item(2)->nodeValue 
    that should help me look at only the third list item!


Bookmarks

Posting Permissions

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