0

可能重复:
在 PHP 中获取标签内容的正则表达式帮助

首先,请不要评论使用正则表达式解析 html。我知道这是不可能的,但在这种情况下它应该完成它的工作。

我尝试获取<country lan="x">...</country>标签的内容。没有特殊情况<country />,PHP DOM Parser 由于包含许多特殊字符(MediaWiki 文本)的标签内容而失败。

所以我有一些文字

    <country lan="en">


    dsadasd


    {|,'''""" }}|]][][]//\\\\\2r2erfaf<>><<<#<div> --..,;;"!"§$%&/()=?`´´``***+~~~''

    0131ß

    ÄÜÖ#ax
    </country>

我目前的解决方案是$pattern = <country lan=\"en\">(.|\t|\r|\n|\s)*<\/country>使用

preg_match_all($pattern, $content, $matches);
print_r($matches);

但打印的结果只是一个空数组。如何仅提取<country lan="x">...</country>标签之间的字符串?

4

1 回答 1

1

用这个

preg_match_all('/<country.*?>(.*?)<\/country>/s', $contents,$hits);
print_r($hits);
于 2012-11-23T09:50:02.850 回答