删除锚标记并使用正则表达式以数组形式获取内部文本
Remove anchor tag and get inner text in an array form using regular expression
我使用这个代码删除锚标记,也只得到内部文本使用表达式
<ul class="alpha">
<li><h3><a href="http://www.overstock.com/Electronics/Computers-Tablets/473/dept.html?TID=TN:ELEC:Comp">Computers & Tablets</a></h3></li>
<li><a href="http://www.overstock.com/Electronics/2-in-1s/28195/subcat.html?TID=TN:ELEC:2in1">2-in-1s</a></li>
<li><a href="http://www.overstock.com/Electronics/Laptops/133/subcat.html?TID=TN:ELEC:Lap">Laptops</a></li>
</ul>
表达式是:
echo preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $str);
输出是:
Computers & Tablets
2-in-1s
Laptops
我们可以得到内部文本内锚标记在数组形式使用正则表达式?请分享你的想法。
我不喜欢你使用HTML与regex而是使用DomDocument但如果你想使用regex比你可以使用preg_match_all
preg_match_all('/(?:(<a.*?>))(.*?)(?=<'/a>)/', '<ul class="alpha">
<li><h3><a href="http://www.overstock.com/Electronics/Computers-Tablets/473/dept.html?TID=TN:ELEC:Comp">Computers & Tablets</a></h3></li>
<li><a href="http://www.overstock.com/Electronics/2-in-1s/28195/subcat.html?TID=TN:ELEC:2in1">2-in-1s</a></li>
<li><a href="http://www.overstock.com/Electronics/Laptops/133/subcat.html?TID=TN:ELEC:Lap">Laptops</a></li>
</ul>',$res);
print_r($res[0]);
输出:Array
(
[0] => Computers & Tablets
[1] => 2-in-1s
[2] => Laptops
)
由于您使用了jQuery标记,所以我更倾向于在jQuery中执行此操作:
var values = [];
$('.alpha').find('a').each(function(index){
values.push($(this).text());
});
此代码获取.alpha类中的所有链接并将它们压入values
数组。values
的输出为:
0: "Computers & Tablets"
1: "2-in-1s"
2: "Laptops"
相关文章:
- 从json子数组获取数据
- 从javascript数组获取php的日期时间
- 从表中的 JSON 数组获取数据
- 每个表的2D数组获取坐标JTSL,JAVASCRIPT
- javascript从元素到数组获取文本
- 如何从数组获取链接以在youtube Iframe API中播放
- JSON 编码多维数组获取值
- 如何从两个 JSON 数组获取增量
- 通过单击按钮数组获取隐藏 html 元素值的当前数组
- 在 JavaScript 中从内部数组获取数据
- 从数组获取非重复索引在 0 时失败
- JavaScript 从对象中的数组获取数组
- 如何通过 ajax 将 php 数组作为 javascript 数组获取
- 快速从 2D 数组获取索引,无需迭代 jquery
- 从 Nodejs socketio 数组获取所有套接字 id
- 无法动态从 json 数组获取数据
- 如何在javascript中使用数组获取和设置html标签的值
- jQuery对象数组获取一个项作为jQuery对象
- 如何在不向输出数组获取空值的情况下拆分字符串
- 从输入到数组获取数据