如何用PHP替换段落中的单词?

时间:2015-02-06 10:32:26

标签: 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>".

我该怎么办?

6 个答案:

答案 0 :(得分:1)

只需在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

希望这有助于你

答案 1 :(得分:1)

使用PHP的strip_tags($string);功能。

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

答案 2 :(得分:1)

如果您只想删除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。

答案 3 :(得分:0)

您正在寻找的是PHP内置的条带标记功能: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"

答案 4 :(得分:0)

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知识,例如:Text Manipulation Tools

答案 5 :(得分:0)

在php中编写一个带有三个文本参数的函数。

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

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