我有一个页面,在其代码的不同部分中有多次<br>foo<br>
示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
The quick brown <br>foo<br> fox jumped over the lazy <br>foo<br> dog.
</body>
</html>
但我需要页面说明:
那只敏捷的棕色狐狸跳过那只懒狗
而不是:
快速棕色
foo
狐狸跳过懒惰的人
foo
狗
我需要它来不断删除<br>foo<br>
,即使我后来添加了更多的文本和<br>foo<br>
请提供一个完整的功能代码,希望php
其他信息:
我正在通过php includes加载大部分文本
我不想在php中声明原始字符串,我希望php检查html代码并删除指定的字符串。
除非字符串是由PHP输出的,否则您无法使用PHP执行您所要求的操作。。。
我想你可以使用javascript;例如(使用JQuery(:
$(document).ready(function(){
var old_body = $('body').html();
var new_body = old_body.replace(new RegExp("<br>foo<br>", "g"),'');
$('body').html(new_body);
});
然而,如果您使用已经描述的方法之一并通过PHP传递内容,那就更好了。
已更新为实际工作。。。
我对PHP不太熟悉,但这似乎可以工作:
str_replace ( "<br>foo<br>", "" , a string containing your html document)
<?php
$your_html = 'The quick brown <br>foo<br> fox jumped over the lazy <br>foo<br> dog.';
$your_html = preg_replace("/<br>foo<br>/", "", $your_html);
echo $your_html;
工作演示
如果将文件加载到php中的字符串中,则可以执行:
$cleaned = str_replace("<br>foo<br>", "", $raw);
使用str_replace
str_replace('<br>foo<br>','',$textInHere);