php str_replace 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7537478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
str_replace not working
提问by moondog
I want to delete all occurrences of this substring from my html file:
我想从我的 html 文件中删除所有出现的这个子字符串:
<span style="font-size: 12pt;">BLANK PAGE</span>
I tried str_replace, thinking that would be a simple solution, but it does not work:
我尝试了 str_replace,认为这是一个简单的解决方案,但它不起作用:
$html = str_replace('<span style="font-size: 12pt;">BLANK PAGE</span>', '', $html);
Any suggestions?
有什么建议?
UPDATE: Mystery solved! Thanks to everyone for letting me know that this shouldwork. Turns out the problem had nothing to do with str_replace! I had grabbed the html string from firebug, not realizing that firebug inserts spaces to "prettify" the html. That's why str_replace failed to find this exact pattern. I would ideally like to delete this question, since the problem ended up having nothing to do with str_replace. Is that possible?
更新:谜团解开!感谢大家让我知道这应该有效。原来问题与 str_replace 无关!我从 firebug 中获取了 html 字符串,没有意识到 firebug 插入空格来“美化” html。这就是 str_replace 未能找到这个确切模式的原因。理想情况下,我想删除这个问题,因为问题最终与 str_replace 无关。那可能吗?
回答by Amber
str_replace()
returns the new version - you need to assign it back to the variable (or a new variable):
str_replace()
返回新版本 - 您需要将其分配回变量(或新变量):
$myhtml = str_replace('<span style="font-size: 12pt;">BLANK PAGE</span>', '', $myhtml);
回答by Howard
It should work that way. Did you perhaps forget to assign the result back to your variable?
它应该这样工作。您是否忘记将结果分配回您的变量?
$myhtml = str_replace('<span style="font-size: 12pt;">BLANK PAGE</span>', '', $myhtml);
回答by Ian Milanowski Mcloughlin
my problem was that i was using de str_replace to replace special characters like (á,é,í,ó,ú) and it did not work . the i tried using notepad++ an change the file enconding to utf8-without BOM , uploaded the file to the server and it worked!
我的问题是我使用 de str_replace 来替换特殊字符,如 (á,é,í,ó,ú) 但它不起作用。我尝试使用记事本 ++ 更改文件编码为 utf8-without BOM ,将文件上传到服务器并且它工作了!
回答by Akilan
you cannot replace the html tags using str_replace. It replaces the exact occurances only. If you want replace the span text "strip_tags" to remove all the tags and change span text using str_replace.
您不能使用 str_replace 替换 html 标签。它只替换确切的出现。如果要替换跨度文本“strip_tags”以删除所有标签并使用 str_replace 更改跨度文本。