【发布时间】:2013-11-09 19:30:20
【问题描述】:
我使用 preg_match() 来检查来自 XML 提要的字符串(即:$resp = simplexml_load_file($API);),它返回超过 1000 个项目,并且使用 preg_match 我从中提取了一些数据每个存储在 $matches 中的项目,但我不知道如何利用 preg_match 存储在 $matches 中的内容
这是我所拥有的以及我尝试过的。
注意:我有 print_r($matches);只是这样我可以在修改 preg 模式时看到结果。
$matches;
preg_match('/(?<=\s|^)[a-zA-Z]{5,19} ?-?\d\d\d\d\d\d\d\d?*(?=\s|$)/', $Apples, $matches);
print_r($matches);
/*Note: $matches returns an array as such: Array ( [0] => Stringdata ) Array ( [0] => moreStringdata ) Array ( [0] => stillmoreStringData ) Array ( [0] => evenmoreStringData ) Array ( [0] => moreStringDataStill )... and I'm just wanting to use array[0] from each in the $results string which is output to the screen */
$results.= "<div class='MyClass'><a href=\"$link\"><img src=\"$linktopicture\"></a><a href=\"$linktopageaboutapples\">$matches</a></div>";
我还在 $results 字符串中尝试了 $matches()、$matches[] 和 $matches[0],但没有任何效果,因为我对使用数组不太了解,所以我想我会问是否有人愿意'不介意让我直接了解可能非常初级的东西,我会非常感激,我提前感谢大家。
【问题讨论】:
-
还有
$matches的内容是什么? -
请在您的代码示例中添加 $Pattern 的定义(并更正错字)并将 print_r($matches) 的输出添加到问题中。
-
@Arkanon 为什么有人需要 Pattern 的定义? (除了我的竞争对手)我只需要知道如何使用存储在 $matches 中的数据。
-
@Arkanon 错字已更正并添加了 $matches 示例