Reputation: 89
I am using a very basic html form with submit button which calls my PHP which handles the input. The problem is my PHP will not recognize the " mark when I try to str_replace. Here is my PHP: (Note- the html text field input is pasted in from google docs)
<?php
$article = $_POST['article'];
//This contains: “This has quotes” from my html input field
echo str_replace("”", "quote", $article);
//Returns the input, without quote replacement
echo addslashes($article);
//Does not add slashes
$quotes = "“This has quotes”";
echo str_replace("“", "quote", $quotes);
//Returns quoteThis has quotesquote as it should
?>
So, what about the HTML input is causing the input quote to not be recognized, and is there a way to fix this problem? Thank you!
Upvotes: 0
Views: 857
Reputation: 78
I can see immediatley you have alot of varied quotes in play. To start with there are vertical single and double quotes " and ', and also left and right version of them also.
For this code your going to have to keep an eye on the encapsulating characters. So....
<?php
$article = $_POST['article']; //This contains: “This has quotes” from my html input field
$article = str_replace('”', 'quote1', $article); // remove double left quote
$article = str_replace('"', 'quote2', $article); // remove double vertical quote
$article = str_replace("'", 'quote3', $article); // remove single vertical quote
echo addslashes($article); //Does not add slashes
$quotes = "“This has quotes”";
echo str_replace("“", "quote", $quotes); //Returns quoteThis has quotesquote as it should
?>
You will notice in the above example, only 3 of the 6 basic quotes are managed, if you want to replace them all you will need to add a few new lines, shown below.
<?php
// REPLACE USING CHARACTER STRING
$article = str_replace('"', 'double vertical', $article); // replace double vertical
$article = str_replace("'", 'single vertical', $article); // replace single vertical
$article = str_replace('‘', 'single left', $article); // replace single left
$article = str_replace('’', 'single right', $article); // replace single right
$article = str_replace('“', 'double left', $article); // replace double left
$article = str_replace('”', 'double right', $article); // replace double right
// REPLACE USING CHARACTER STRING
// REPLACE USING CHATACTER CODE
$article = str_replace(chr(34), 'double vertical', $article); // replace double vertical
$article = str_replace(chr(39), 'single vertical', $article); // replace single vertical
$article = str_replace(chr(145), 'single left', $article); // replace single left
$article = str_replace(chr(146), 'single right', $article); // replace single right
$article = str_replace(chr(147), 'double left', $article); // replace double left
$article = str_replace(chr(148), 'double right', $article); // replace double right
// REPLACE USING CHATACTER CODE
?>
The above examples here show two such ways of performing these replacements, one using a character string and the other using the chatacters number. Either will do in this situation, but character code is often easier to look at.
Upvotes: 1