如何使用 php 从数组中的所有锚点标签中获取国家、链接、文本



如何使用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 ... */

我认为您正在寻找服务器端JQuery,实际上存在!

结帐 phpQuery

使用示例

在您的特定情况下,它可以像这样使用:

<?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,它是一个文件!!

最新更新