使用 preg_replace_callback 函数替换结束 div 标签
我正在尝试开发一个 PHP 脚本,将 HTML 字符串中的所有 div 替换为段落,除了那些具有属性的段落(例如
)。我的脚本当前要做的第一件事是使用简单的 str_replace() 将所有出现的
替换为
,这会留下所有 div 标签带有属性和结束 div 标签 (
标记会出现一些问题。到目前为止,我已经开发了一个 preg_replace_callback 函数,旨在将一些 标签转换为
标签以匹配开头的
< ;p>
标签,但当其他 标签以属性结尾
时,忽略它们。下面是我正在使用的脚本;<?php
$input = "<div>Hello world!</div><div><div id=\"1\">How <div>are you</div> today?</div></div><div>I am fine.</div>";
$input2 = str_replace("<div>", "<p>", $input);
$output = preg_replace_callback("/(<div )|(<\/div>)/", 'replacer', $input2);
function replacer($matches){
static $count = 0;
$counter=count($matches);
for($i=0;$i<$counter;$i++){
if($matches[$i]=="<div "){
return "<div ";
$count++;
} elseif ($matches[$i]=="</div>"){
$count--;
if ($count>=0){
return "</div>";
} elseif ($count<0){
return "</p>";
$count++;
}
}
}
}
echo $output;
?>
该脚本基本上将所有剩余的
和
标记时递增,或者在数组内遇到
标签,否则返回
。 脚本的输出应该是;<p>Hello world!</p><p><div id="1">How <p>are you</p> today?</div></p><p>I am fine.</p>"
相反,我得到的输出是;
<p>Hello world!</p><p><div id="1">How <p>are you</p> today?</p></p><p>I am fine.</p>
我花了几个小时对脚本进行了尽可能多的编辑,但我一直得到相同的输出。谁能向我解释我哪里出了问题或提供替代解决方案?
任何帮助将不胜感激。
I am trying to develop a PHP script that replaces all divs in an HTML string with paragraphs except those which have attributes (e.g. <div id="1">
). The first thing my script currently does is use a simple str_replace() to replace all occurrences of <div>
with <p>
, and this leaves behind any div tags with attributes and end div tags (</div>
). However, replacing the </div>
tags with </p>
tags is a bit more problematic.
So far, I have developed a preg_replace_callback function that is designed to convert some </div>
tags into </p>
tags to match the opening <p>
tags, but ignore other </div>
tags when they are ending a <div>
with attributes. Below is the script that I am using;
<?php
$input = "<div>Hello world!</div><div><div id=\"1\">How <div>are you</div> today?</div></div><div>I am fine.</div>";
$input2 = str_replace("<div>", "<p>", $input);
$output = preg_replace_callback("/(<div )|(<\/div>)/", 'replacer', $input2);
function replacer($matches){
static $count = 0;
$counter=count($matches);
for($i=0;$i<$counter;$i++){
if($matches[$i]=="<div "){
return "<div ";
$count++;
} elseif ($matches[$i]=="</div>"){
$count--;
if ($count>=0){
return "</div>";
} elseif ($count<0){
return "</p>";
$count++;
}
}
}
}
echo $output;
?>
The script basically puts all the remaining <div>
and </div>
tags into an array and then loop through it. A counter variable is then incremented when it encounters a <div>
tag or decremented when it encounters a </div>
within the array. When the counter is less than 0, a </p>
tag is returned, otherwise a </div>
is returned.
The output of the script should be;
<p>Hello world!</p><p><div id="1">How <p>are you</p> today?</div></p><p>I am fine.</p>"
Instead the output I am getting is;
<p>Hello world!</p><p><div id="1">How <p>are you</p> today?</p></p><p>I am fine.</p>
I have spent hours making as many edits to the script as I can think of, and I keep getting the same output. Can anyone explain to me where I am going wrong or offer an alternative solution?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 mario 评论的旁边,与 phpquery 或 querypath 类似,您可以使用 PHP
DOMDocument
类来搜索有问题的元素并将其替换为
元素。
基石是 DOM(文档对象模型)和 XPath:
这将为您提供:
Next to what mario commented, comparable to phpquery or querypath, you can use the PHP
DOMDocument
class to search for the<div>
elements in question and replace them with<p>
elements.The cornerstones are the DOM (Document Object Model) and XPath:
This will give you:
我对多个正则表达式采取了不同的方法:
这会给你带来:
如果你不需要代码片段,我至少自己学到了一些关于正则表达式的知识:P
I took a different approach with multiple regular expressions:
This will get you:
If you don't need the snippet, I have learned something about regexp's myself at least :P