2014-12-02 175 views
0

我有一个HTML字符串,并且想要提取特定标记<selection>内的内容。正则表达式匹配模式php

例子:

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标签文本,但所有的标签都有不同的属性。请帮助我解决方案。

+5

为什么不使用html解析器? – 2014-12-02 12:16:14

+1

这个'<(选择)\ b [^ <>] *>([^ <>] *)<\/\1>'将适用于您当前的输入。从组索引2获取想要的字符串。 – 2014-12-02 12:17:36

+0

嵌套在''标签中的标签是否可以嵌套?像' foo bar baz'? – 2014-12-02 12:19:41

回答

2

试试这个:

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

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

+0

谢谢!它的作用像一个魅力! – Mohanraj 2014-12-03 04:11:29

0

你可以试试这个:

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

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

为了让<selection>...</selection>之间标签:

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

如果name属性包含“>>><<<'会发生什么? – Toto 2014-12-02 13:12:52