PHP str_replace 不能正常工作

发布于 2024-11-07 17:44:14 字数 1291 浏览 6 评论 0原文

我有两个字符串,它们是从文件中提取的行。我试图首先通过执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡墨 2024-11-14 17:44:14

str_replace() 返回修改后的字符串。所以你需要分配它:

$title_new = str_replace("<title>"," ", $title_new);

与<​​code>$desc_new相同。阅读文档了解更多详细信息。

str_replace() returns the modified string. So you need to assign it:

$title_new = str_replace("<title>"," ", $title_new);

Same with $desc_new. Read the docs for more detail.

心不设防 2024-11-14 17:44:14

str_replace 返回字符串,因此您应该这样做:

$title_new = str_replace("<title>"," ", $title_new);
$title_new = str_replace("</title>"," ", $title_new);
$desc_new = str_replace("<description>"," ", $desc_new);
$desc_new = str_replace("</description>"," ", $desc_new);

str_replace returns the string, so you should have done this:

$title_new = str_replace("<title>"," ", $title_new);
$title_new = str_replace("</title>"," ", $title_new);
$desc_new = str_replace("<description>"," ", $desc_new);
$desc_new = str_replace("</description>"," ", $desc_new);
原谅过去的我 2024-11-14 17:44:14
 $title_new = str_replace(array("<title>", "</title>")," ", $file_lines[$title_line]);
 $desc_new = str_replace(array("<description>","</description>")," ", $file_lines[$desc_line]);

或者使用

条带标签

 $title_new = str_replace(array("<title>", "</title>")," ", $file_lines[$title_line]);
 $desc_new = str_replace(array("<description>","</description>")," ", $file_lines[$desc_line]);

Or use

strip_tags

深海里的那抹蓝 2024-11-14 17:44:14

使用 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.

夕色琉璃 2024-11-14 17:44:14

正如其他答案所提到的,您需要分配返回值,例如 $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> 达到其预期目的。

$buffer = strip_tags($buffer, '<title><description>')

此外,可能没有必要逐行解析文件。使用诸如 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 use strip_tags() for its intended purpose.

$buffer = strip_tags($buffer, '<title><description>')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文