preg_replace 删除一些不需要的 div
在下面的代码中,我需要删除 .
它们将始终位于字符串的开头和结尾,例如:
<div class="grid_8"><img src="http://rps.sanscode.com/site/assets/media/images/rps_mini_logo.png" border="0" alt="Rapid Print Solutions" style="margin-bottom: 30px;" />
<h1></h1>
</div>
What is a合适的正则表达式让 preg_replace 删除它? 8 可以是 1 到 16 之间的任何数字。
谢谢
Jason
@Amjad...
这是我的代码,
public function fix_grid(){
$result = db::query("select * from sc_content_components where component_value_1 like '%grid_%'")->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $item){
$json = json_decode($item['component_value_1']);
if(is_null($json)) continue;
$x = reset($json);
echo htmlspecialchars($x);
echo "<p>=======================<b>Changes to: </b></p>";
$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
, '/<\/(\div+)[^>]*>$/');
$x = preg_replace($patterns, array('',''), trim($x));
echo htmlspecialchars($x);
echo "<hr>";
$json[0]=$x;
// $ne['component_value_1'] = json_encode($json);
// db::where('component_id', $item['component_id']);
// db::update('sc_content_component', $ne);
}
}
我正在使用下面的正则表达式 (@Amjad Masad),它不会删除最后一个 div。
正如你所看到的,我正在使用修剪,但它似乎不起作用
In the following code, I need to remove <div class="grid_8"></div>.
They will always be at the start and finish of my string, for example:
<div class="grid_8"><img src="http://rps.sanscode.com/site/assets/media/images/rps_mini_logo.png" border="0" alt="Rapid Print Solutions" style="margin-bottom: 30px;" />
<h1></h1>
</div>
What is a suitable regex for preg_replace to remove it? 8 can be any number between 1 and 16.
Thanks
Jason
@Amjad...
Here is my code
public function fix_grid(){
$result = db::query("select * from sc_content_components where component_value_1 like '%grid_%'")->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $item){
$json = json_decode($item['component_value_1']);
if(is_null($json)) continue;
$x = reset($json);
echo htmlspecialchars($x);
echo "<p>=======================<b>Changes to: </b></p>";
$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
, '/<\/(\div+)[^>]*>$/');
$x = preg_replace($patterns, array('',''), trim($x));
echo htmlspecialchars($x);
echo "<hr>";
$json[0]=$x;
// $ne['component_value_1'] = json_encode($json);
// db::where('component_id', $item['component_id']);
// db::update('sc_content_component', $ne);
}
}
I'm using the regex below (@Amjad Masad) and it doesn't remove the last div.
As you can see I am using trim and it doesn't seem to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于所述问题,解决方案如下:
编辑:扩展正则表达式 =
replacement =
"$2"
For the stated problem, this is the solution:
Edit: expanded regex =
replacement =
"$2"
对于开放
For the open
简单一些。
如果 1 到 16 很重要并且是更大范围的一部分...
如果 1 到 16 是唯一可能的范围,因此不相关...
此致。
Somewhat simpler.
If the 1 to 16 is significant and part of a greater range...
If the 1 to 16 is the only possible range and therefore irrelevant...
Regards.