我编写了一个脚本,它从html字符串中获取特定元素。一切正常,而脚本获得不必要的价值。我只想下载第二个值(。*?)。如何修改表达式以仅获取第二个值跳过其余值?。
我的剧本:
<?php
$html = '
<tr><td>AD - Andorra<td>CA - Canada
<tr><td>AE - United Arab Emirates<td>PR - Puerto Rico
<tr><td>AF - Afghanistan<td>US - United States of America
<tr><td>AG - Antigua and Barbuda<td>
';
preg_match_all('/<td>(.*?)<td>(.*?)\n/s', $html, $value);
print_r($value);
?>
答案 0 :(得分:1)
虽然有很多方法可以让这只猫受到伤害,而且大多数人会坚持认为正则表达式解决方案是绝对禁止的,但在我看来,你已经存在,你的代码会在{{1}产生正确的结果 - 一个包含第二个捕获括号的值的数组。这是执行代码的psysh会话 -
$value[2]
您可以修改正则表达式以仅捕获第二列,方法是将第一列转换为非捕获括号>>> $html = '
<tr><td>AD - Andorra<td>CA - Canada
<tr><td>AE - United Arab Emirates<td>PR - Puerto Rico
<tr><td>AF - Afghanistan<td>US - United States of America
<tr><td>AG - Antigua and Barbuda<td>
;
preg_match_all('/<td>(.*?)<td>(.*?)\n/s', $html, $value);
print_r($value);
... ... ... ... ... => """
\n
<tr><td>AD - Andorra<td>CA - Canada\n
<tr><td>AE - United Arab Emirates<td>PR - Puerto Rico\n
<tr><td>AF - Afghanistan<td>US - United States of America\n
<tr><td>AG - Antigua and Barbuda<td>\n
"""
>>> => 4
>>> Array
(
[0] => Array
(
[0] => <td>AD - Andorra<td>CA - Canada
[1] => <td>AE - United Arab Emirates<td>PR - Puerto Rico
[2] => <td>AF - Afghanistan<td>US - United States of America
[3] => <td>AG - Antigua and Barbuda<td>
)
[1] => Array
(
[0] => AD - Andorra
[1] => AE - United Arab Emirates
[2] => AF - Afghanistan
[3] => AG - Antigua and Barbuda
)
[2] => Array
(
[0] => CA - Canada
[1] => PR - Puerto Rico
[2] => US - United States of America
[3] =>
)
)
=> true
:(注意第一次打开'/<td>(?:.*?)<td>(.*?)\n/s'
后添加的?:
。期望的结果位于(
然后。修改后的代码执行:
$value[1]
答案 1 :(得分:0)
您可以将字符串拆分为数组。 http://php.net/manual/en/function.explode.php