开发者

How do I generate an array of the items from a list (ul) tag?

From the following code:

<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol>

How do I create an array using phpQuery

array(
  'Coffee',
  'Tea',
  'Milk'
);

Here's my first attempt, it is very ugly

    $d开发者_JAVA技巧oc = phpQuery::newDocumentHTML(...);
    $img = $doc->find('ol');
    $list = array();
    function attrsrc($i, $v){
        global $list;
        $list[] =  phpQuery::pq($v)->text();
    }
    phpQuery::each($img, 'attrsrc',  new CallbackParam, new CallbackParam);
    print_r($list);


Try this:

include 'phpQuery.php';

$string = '<ol> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol>';
$content = phpQuery::newDocument($string)->find('ol li');

$drinks = array();

foreach ($content as $li) {
     $drinks[] = pq($li)->text();
}

print_r($drinks);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜