[英]How can I remove the <p style=“text-align:center”> (or left, right. The paragraph comes from database) using PHP?
我有来自数据库的html代码(我使用TinyMCE保存数据)
<p style="text-align: center;">Come on grab your friends</p>
<p style="text-align: center;">Go to very distant lands</p>
<p style="text-align: center;">Jake the Dog and Finn the Human</p>
<p style="text-align: center;">The fun never ends</p>
<p style="text-align: center;"><strong>Adventure Time!</strong></p>
考虑到使用TinyMCE时还可以应用其他样式,如何删除那些<p></p>
标签?
要从字符串中删除HTML标签,可以使用strip_tags() :
$str = '<p style="text-align: center;">Come and grab your friends</p>';
$str2 = strip_tags($str);
echo $str2; // "Come and grab your friends"
要保留某些标签,可以添加其他参数:
$str = '<p style="text-align: center;"><strong>Adventure Time!</strong></p>';
$str2 = strip_tags($str, "<strong>"); // Preserve <strong> tags
echo $str2; // "<strong>Adventure Time!</strong>"
第二个参数是一个字符串,列出了您不想剥离的每个标签,例如:
$str2 = strip_tags($str, "<p><h1><h2>"); // Preserve <p>, <h1>, and <h2> tags
有关更多信息,请查看上面链接的PHP文档。
尽管您提到您不使用js,但我强烈建议您开始使用它。 您会发现在很多情况下仅干扰客户端而不是仅使用服务器端过程(如php)非常有用。 因此,仅作记录,这是我建议的jQuery解决方案:
<html>
<head>
<!-- your head content here -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<p style="text-align: center;">Come on grab your friends</p>
<p style="text-align: center;">Go to very distant lands</p>
<p style="text-align: center;">Jake the Dog and Finn the Human</p>
<p style="text-align: center;">The fun never ends</p>
<p style="text-align: center;"><strong>Adventure Time!</strong></p>
<div id="result"></div> <!-- here I have added an extra empty div to display the result -->
<script>
$(document).ready(function() {
$("p").each(function() {
var value = $(this).text();
$("#result").append(value+ "<br>");
$(this).css("display", "none");
});
});
</script>
</body>
</html>
此处的实时示例: http : //jsfiddle.net/Rykz9/1/
希望您(和其他人)发现它有用...祝您编程愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.