[英]Split html links tag
我在字符串中有以下html代码:
Hello world
<img src="mypicture.png" />
<p>Some text in a tag</p>
<a href="http://www.google.fr">Link to google</a> Some Text <a href="http://www.yahoo.fr">Link to yahoo</a> End of line
<p>Some text in a tag</p>
<a attribute="some value" href="http://www.apple.com">Link to apple</a>
Some text
我想将此字符串转换为以下数组:
0 => Hello world
<img src="mypicture.png" />
<p>Some text in a tag</p>
<a href="
1 => http://www.google.fr
2 => ">Link to google</a> Some Text <a href="
3 => http://www.yahoo.fr
4 => ">Link to yahoo</a> End of line
<p>Some text in a tag</p>
<a attribute="some value" href="
5 => http://www.apple.com
6 => ">Link to apple</a>
Some text
我已经尝试过此正则表达式。 提取链接效果很好,但是我无法建立数组...
<a (.*?)href=(.*?)\"(.+?)\"(.*?)>
您也可以添加一些内容以捕获链接之前的所有内容:
([\\W\\w]*?)(?:(<a .*?href=.*?\\")(.+?)(?=\\")|$)
然后,您只需要逐步完成每个匹配,并将pre + link
添加到数组,并将url
到数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.