正则表达式匹配模式php

时间:2014-12-02 12:15:32

标签: php regex preg-replace preg-match-all

我有一个HTML字符串,想要在特定标记<selection>中提取内容。

示例:

Lorem Ipsum is simply <selection alt="dummy" name="dummy">dummy</selection > text the printing and typesetting <selection alt="industry" name="industry">industry</selection>. Lorem Ipsum has been the industry's <selection alt="standard" name="standard">standard</selection>dummy text ever since the 1500s.

在上面的HTML字符串中,我需要在selection标记内提取文本,但所有标记都有不同的属性。请帮我解决。

2 个答案:

答案 0 :(得分:2)

试试这个:

preg_match_all('/<selection.*?">(.*?)<\/selection.*?>/is',$sourcestring,$matches);

$ matches是数组形式的结果。 我希望这对你有所帮助。

答案 1 :(得分:0)

你可以试试这个:

/<selection[^>]*>([^<]*)/g

http://regex101.com/r/pS3sM0/1

允许<selection>...</selection>之间的标记:

/<selection[^>]*>(.*?)(?:<\/selection)/g