如何在PHP中替换段落中的单词


how to replace a word from a paragraph ,in PHP?

我有一段

"hi boss how is your health. <p><div> my office is big Perl </div></p>"

我想单独删除<div></div>标签,这样更新后的字符串将看起来像

"hi boss how is your health. <p>my office is big Perl</p>".

我该怎么办?

使用PHP的strip_tags($string);函数。

更多参考:http://php.net/strip_tags

只需在php中使用strip_tags函数即可移除。使用下方的代码

<?php
$string = "hi boss how is your health. <div> my office is big Perl </div>";
echo strip_tags($string,"<p>");  // Prints "hi boss how is your health. my office is big Perl"
?>

如果需要,可以允许一些或多个标记。请阅读此处的完整参考资料http://php.net/strip_tags

希望这能帮助您

如果你只想删除div标记,那么使用下面这样的str_replace-

$string = "hi boss how is your health. <p><div>my office is big Perl</div></p>";
$string = str_replace('<div>' , '' , $string);
$string = str_replace('</div>' , '' , $string);

如果你想删除所有的html标签,那么根据其他成员的建议使用strip_tags。

PHP方式,使用str_replace如下:

<?PHP
$string = "hi boss how is your health. <p><div>my office is big Perl</div></p>";
$string=str_replace('<div>', '', $string);
$string=str_replace('</div>', '', $string);
echo $string;
?>

简单的方法,访问任何在线文本操作工具网站并编辑整个文本段落一次,无需任何PHP知识,例如:文本操作工具

您想要的是PHP中内置的strip-tags函数:http://php.net/strip_tags它从输入字符串中剥离html标签,例如

$text = "hi boss how is your health. <div> my office is big Perl </div>";
echo strip_tags($text);
//Will echo "hi boss how is your health. my office is big Perl"

用php编写一个函数,该函数接受三个文本参数。

  • 第一个参数是将要处理的文本
  • 第二个参数是要替换的字符串
  • 第三个参数是将放置在新字符串中的字符串

函数在第一个文本参数中搜索第二个文本参数如果找到第三个参数,则将其替换为文本。更换后,该功能将返回要打印在屏幕上的新文本。