Reputation:
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\">";
Upvotes: 1
Views: 2450
Reputation: 309
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.
Upvotes: 1
Reputation: 968
$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
Upvotes: 1
Reputation: 2213
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);
Upvotes: 2
Reputation: 19220
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.
Upvotes: 1
Reputation: 72991
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.
Upvotes: 4