PHP str_replace 不能正常工作
我有两个字符串,它们是从文件中提取的行。我试图首先通过执行 str_replace 并用空格替换它们来剥离它们的标签(例如 )。不幸的是,这似乎不起作用,因为当我在表单中回显结果时,我仍然看到标签。
有什么想法吗?
# Trim the title and description of their tags. Keep only text.
$title_new = $file_lines[$title_line];
str_replace("<title>"," ", $title_new);
str_replace("</title>"," ", $title_new);
$desc_new = $file_lines[$desc_line];
str_replace("<description>"," ", $desc_new);
str_replace("</description>"," ", $desc_new);
# Echo out the HTML form
echo "<form action=\"rewrite.php\" method=\"post\">";
echo "Title: <input type=\"text\" name=\"new_title\" size=\"84\" value=\"".$title_new."\">";
echo "</input>";
echo "<br>Article Body:<br>";
echo "<textarea rows=\"20\" cols=\"100\" wrap=\"hard\" name=\"new_desc\">".$desc_new."</textarea><br>";
echo "<input type=\"hidden\" name=\"title_line\" value=\"".$title_line."\">";
echo "<input type=\"hidden\" name=\"title_old\" value=\"".$file_lines[$title_line]."\">";
echo "<input type=\"hidden\" name=\"desc_line\" value=\"".$desc_line."\">";
echo "<input type=\"hidden\" name=\"desc_old\" value=\"".$file_lines[$desc_line]."\">";
echo "<input type=\"submit\" value=\"Modify\" name=\"new_submit\">";
I've got two strings that are lines pulled from a file. I'm trying to first strip them of their tags (like ) by doing a str_replace and replacing them with a space. Unfortunately, this doesn't seem to be working because when I echo out the results in the form I still see the tags.
Any ideas?
# Trim the title and description of their tags. Keep only text.
$title_new = $file_lines[$title_line];
str_replace("<title>"," ", $title_new);
str_replace("</title>"," ", $title_new);
$desc_new = $file_lines[$desc_line];
str_replace("<description>"," ", $desc_new);
str_replace("</description>"," ", $desc_new);
# Echo out the HTML form
echo "<form action=\"rewrite.php\" method=\"post\">";
echo "Title: <input type=\"text\" name=\"new_title\" size=\"84\" value=\"".$title_new."\">";
echo "</input>";
echo "<br>Article Body:<br>";
echo "<textarea rows=\"20\" cols=\"100\" wrap=\"hard\" name=\"new_desc\">".$desc_new."</textarea><br>";
echo "<input type=\"hidden\" name=\"title_line\" value=\"".$title_line."\">";
echo "<input type=\"hidden\" name=\"title_old\" value=\"".$file_lines[$title_line]."\">";
echo "<input type=\"hidden\" name=\"desc_line\" value=\"".$desc_line."\">";
echo "<input type=\"hidden\" name=\"desc_old\" value=\"".$file_lines[$desc_line]."\">";
echo "<input type=\"submit\" value=\"Modify\" name=\"new_submit\">";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
str_replace() 返回修改后的字符串。所以你需要分配它:
与<code>$desc_new相同。阅读文档了解更多详细信息。
str_replace() returns the modified string. So you need to assign it:
Same with
$desc_new
. Read the docs for more detail.str_replace 返回字符串,因此您应该这样做:
str_replace returns the string, so you should have done this:
或者使用
Or use
使用 PHP 时,剥离标签的最佳方法是 HTMLPurifier
我不会尝试使用 str_replace 做类似的事情。很可能有人会犯错误。
The best way to strip tags, when using PHP, is HTMLPurifier
I wouldn't try doing something like that with str_replace. Most likely one'd make a mistake.
正如其他答案所提到的,您需要分配返回值,例如
$title_new = str_replace(""," ", $title_new);</code>,但我强烈建议您使用 <a href ="http://php.net/manual/en/function.strip-tags.php" rel="nofollow"><code>strip_tags()</code></a> 达到其预期目的。
此外,可能没有必要逐行解析文件。使用诸如 file_get_contents() 之类的方法一次读取整个文件,然后使用正则表达式或 xml 解析器,速度会快很多倍。
As other answers have mentioned you need to assign the return value like
$title_new = str_replace("<title>"," ", $title_new);
, but I highly encourage you to usestrip_tags()
for its intended purpose.Also it's likely unnecessary to parse the file line by line. It would be many times faster to read the whole file at once with something like
file_get_contents()
and then use a regular expression or an xml parser.