You're missing a ; on line 1.
$display_box_content = '<div style="box-sizing: border-box; color: rgb(37, 37, 37); font-family: Axiforma-Regular; font-size: 16px;"></div>'; //added a ; here
$display_box_content = str_replace('"', "'", $display_box_content);
This code actually works; Please see 3v4l
Answer from IsThisJavascript on Stack OverflowYou're missing a ; on line 1.
$display_box_content = '<div style="box-sizing: border-box; color: rgb(37, 37, 37); font-family: Axiforma-Regular; font-size: 16px;"></div>'; //added a ; here
$display_box_content = str_replace('"', "'", $display_box_content);
This code actually works; Please see 3v4l
i was using htmlentites function like this:
$display_box_content = htmlentities($display_box_content);
$display_box_content = str_replace('"', "'", $display_box_content);
so i just swapped those lines to this:
$display_box_content = str_replace('"', "'", $display_box_content);
$display_box_content = htmlentities($display_box_content);
and it worked. Thanks everybody for your help!
First of all, it is always better to check for the unwanted characters and get back to the user, than silently stripping them. Say, a user added a quote to their password, you removed it, and so they won't be able to login at all! So it's better to check and tell the user instead:
if (!ctype_alnum($username)) {
$errors[] = "Only letters and numbers allowed in username";
}
...
if ($errors) {
echo "You've got some errors, please fix them: ". implode("<br>", $errors);
} else {
// proceed with normal flow
But in this specific case I would advise against replacing any characters. Imagine Shaquille "Shaq" O'Neal is going to register on your site. What's the point in stripping him of all those quotes? Let alone the password, where use of punctuation is strongly encouraged.
After all, those quotes don't do any harm, if you properly handle them.
for HTML, simply use
htmlspecialchars()withENT_QUOTESattribute:Your username is: <?= htmlspecialchars($username, ENT_QUOTES) ?><br />for SQL, use prepared statements to avoid any problems with quotes
But just for sake of literal answer, here is how to remove quotes (or any other characters and even multi-character substrings) in PHP
$yourVariable = "scor\"pi'on";
$substringsToRemove = ['\'', '"'];
$yourVariable = str_replace($substringsToRemove, "", $yourVariable);
$keyItem = str_replace ("'","\'",$keyItem);
This replaces a single quote with an 'escaped' single quote \' .
How to replace one single quote by two single quotes in a string with PHP? - Stack Overflow
php string replace quotes - Stack Overflow
Replacing double quotes with single quotes - Post.Byes - Bytes
php - str_replace double and single quotes - Stack Overflow
Update: I'd agree with others that the following is an easier-to-read alternative for most folks:
$page = str_replace("'", '"', $page);
My original answer:
$page = str_replace(chr(39), chr(34), $page);
You don't need to escape the quote character (in fact it is \, not /, unless you were confused with the standard regex delimiters) if the string isn't delimited with the same character.
$page = str_replace("'", '"', $page);
I didn't have a problem with your code, my test is below:
<?php
$input = '"This" is a '."'".'String'."'";
echo $input.'<br />';
//Echos "This" is a 'String'
$output = str_replace('"','\"',$input);
$output = str_replace("'","\'",$output);
echo $output;
//Echos \"This\" is a \'String\'
Edited
Irrelevant now, OP figured it out :D
Try this:
$output = str_replace("\"","\\\"",$input);
$output = str_replace("\'","\\\'",$output);
return $output;
The problem is that ' inside a string, should be noted as \' , as it is an escape character. The backslash \ is a double \ as well inside a string.
Let me know if this works.
Or:
$con = str_replace(chr(34), chr(39), $content);
What you do is correct and should work. If it doesn't, then you may only SEE double quotes, but in reality these are other characters. Possible is html " character rendered as ". There are also several chars very similar to double quotes. hey 'happen' especially when pasting text from word or openoffice. You'll include all possibilities in str_replace (it can take arrays of strings as parameters).
Hi,
the purpose of this is to be used in an article schema markup in the "articleBody" field,
what i have used until now is <?php $examplePost = get_post();
echo preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', wp_strip_all_tags($examplePost->post_content)); ?>
which works PERFECT! that until the article includes double quotes, and when it does, the schema markup picks up on it and throws an error "
JSON-LD
Missing ',' or '}' in object declaration."
which disable the whole schema.
i would prefer to be able to keep (by skip/ignore the double quotes ) or replace the double quotes wish single quote but if not possible, removing them is also alright.
i tried changing to str_replace and other methods i found online but couldnt get it to work.
Help?
PHP's DOMDocument class does this automatically for you. All you need to do is load the HTML and save it back. This might work better than a regex as it's not dependent on the markup format. The downside is that it will fix all the HTML, not just the quotes around attributes that begin with a particular domain.
$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTML($html);
$result = $dom->saveHTML();
Demo
I don't see a use-case for replacing just some particular piece of HTML markup. But I don't know what you're trying to accomplish, so I will offer a regex solution as well:
preg_replace("#\b\w+\s*='((?=http://www\.website\.com)[^']+)'#is",'"$1"',$html);
This uses a positive lookahead to check of the attribute value begins with with the URL you've defined.
Regex101 demo
You can match the following regex:
=\'(http:\/\/www\.website\.com.+?)\'
and replace with:
="\1"
Demo: http://regex101.com/r/wQ7jV5
You haven't provided much context for your question so it's difficult to know exactly how to help, but lets say you have a variable called $title with double quotes in it, you can replace them like this:
$title = str_replace('"', "'", $title);
You can use the str_replace() following :
str_replace('"', "'", $text);
str_replace