如何使用PHP從數組中的所有錨標記獲取國家/地區、鏈接、文本$str ='<div class="main"> <span country="PK"></span><a class="link" href="linkOne">1</a> <span country="US"></span><a class="link" href="linkTwo">2</a> <span country="UA"></span><a class="link" href="LinkThree">3</a> </div>';function getStr($str,$start,$end){ $str = explode($start,$str,2); $str = explode($end,$str[1],2); return $str[0];}$html = getStr($str,'<div class="main">','</div>');preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $html, $result);if (!empty($result)) { echo $result['href'][0];}需要這樣:/* Array */$links[0] = "PK", "linkOne", "1";$links[1] = "US", "linkTwo", "2";/* and so on ... */
1 回答

犯罪嫌疑人X
TA貢獻2080條經驗 獲得超4個贊
我認為您正在尋找服務器端JQuery
,它實際上存在!
使用示例
在您的具體情況下,它可以這樣使用:
<?php
include 'phpQuery.php';
$str = '<span country="US"></span><a href="url-here">some text</a>';
$doc = phpQuery::newDocument($str);
// Note that we can use CSS style selector,
// instead of just "span".
$links = array();
foreach($doc['span'] as $item)
{
? $node = pq($item);
? $sibling = $node->next();
? if ( $sibling->is('a') ) {
? ? $links[] = array(
? ? ? $node->attr('country'),
? ? ? $sibling->attr('href'),
? ? ? $sibling->text(),
? ? );
? }
}
// Display result:
print_r($links);
只需獲取phpQuery-0.9.5.386-onefile.zip,將其解壓并將其重命名為phpQuery.php
,它是一個文件!
- 1 回答
- 0 關注
- 137 瀏覽
添加回答
舉報
0/150
提交
取消