2

我正在尝试使用 PHP 从 xml 页面上的标题中获取主题标签,然后使用减去主题标签的单词作为列表项的 id。到目前为止,我所得到的只有在标题仅包含主题标签时才有效。如果句子有更多内容,它将不会返回任何内容。我需要做什么才能使其正常工作?

preg_match_all('#<title>\#(\w+)</title>#Us', $item, $temp );
4

1 回答 1

2

Here is one approach:

preg_match_all('~<title>(?:[^<#]*\s)?#(\w+)[^<]*</title>~s', $item, $temp);

It allows anything besides < or # between the <title> and the hashtag, provided the hashtag itself is preceded by whitespace; and it will allow anything besides < between the hashtag and the </title>.

Note that this will only match one hashtag per title. If you need to be able to get multiple hashtags from a single title, I recommend that you first use preg_match_all to get the titles, and then, for each title, use preg_match_all to get its hashtag(s), if any.

于 2012-11-30T20:43:57.427 回答