我是一名初学者,并且遇到了使用RegExr工具发现的正则表达式的问题。使用正则表达式匹配PHP中的字符串
我从名为properties.xml中的XML文件,我在这里展示载一组分类广告的标题 -
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<item>
<title>For Sale - Toaster Oven</title>
</item>
<item>
<title>For Sale - Sharp Scissors</title>
</item>
<item>
<title>For Sale - Book Ends</title>
</item>
<item>
<title>For Sale - Mouse Trap</title>
</item>
<item>
<title>For Sale - Water Dispenser</title>
</item>
</channel>
</rss>
这里的,如果有哪个解析XML,然后检查PHP代码火柴;不幸的是它没有显示。
<?php
$xml = simplexml_load_file("properties.xml");
foreach ($xml->channel->item as $item){
$title = $item->title;
$myregex = preg_quote("/(?<=For(.)Sale(.)-(.))[^]+/");
$result = preg_match($myregex, $title, $trim_title);
echo $result;
}
?>
我已经核对过RegExr工具的正则表达式,它似乎很动听 - 这里有一个撷取画面
谢谢,但它也不起作用 - 根据需要选择整个字符串,而不是按照“For Sale - ”字符串出现的内容。这里是RegExr的屏幕截图 - http://i.imgur.com/ygszr.png –
忘记RegExr中的内容或不匹配 - PHP只会为您提供捕获组(即括号)时所要求的内容。正如@ Maks3w指出,你可能需要访问节点的'text()'而不是节点本身。 –