str_replace and htmlspecialchars not working - str-replace

OK, I know that what I see is not necessarily what the code says. I got that part figured out. But I have a long list of characters that need to be stored in a database as their html entities rather than as the punctuation marks. I have updated my code to this:
$searchval = array("á","–","—","/"," ","-","...","…","\&","\'","í","\[","\~","\"","\]");
$replaceval = array("\&egrave\;","\&ndash\;","\&mdash\;","\&#47","\&#nbsp\;","\&#hyphen\;","\&hellip\;","\&hellip\;","\&amp\;","\&apos\;","\&igrave\;","\&lbrack\;","\&ntilde\;","\&quot\;","\&rbrack\;");
str_replace($searchval,$replaceval,$fixed);
But the entitiy names are not going into the database.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'most # rig&ht...'
How do I get the entities to go into the database rather than the punctuation marks?
<!DOCTYPE html>
<html>
<body style="margin-left:50px">
<?php
$fixed = "this is al'most right";
echo $fixed;
echo "<br>";
echo "htmlspecialchars: ";
echo htmlspecialchars($fixed, ENT_QUOTES);
echo "<br>";
echo "str_replace: ";
echo str_replace("'", "&apos;", $fixed);
echo "<br>";
?>
</body>
</html>

Related

Parse error: syntax error, unexpected end of file plzz help me find out whats the error?

0){
while ($row=mysqli_fetch_array($res))
{
$Name=$row['Name'];
$Link=$row['Link'];
?>
">
0){
while ($row=mysqli_fetch_array($res))
{
$submenu=$row['submenu'];
?>
Please share a bigger snippet of code. This error usually happens when there is a typing mistake or not closing php tags. Make sure your <?php ?> tags are opening and closing correctly and check if you are using shorthand tags like this <? } ?> If you are using it please avoid that. Also make sure that you are not mixing PHP opening and closing tags with }?> make sure there is a space between php opening and closing tags.

phpcs require alternative syntax in html files

I want to require alternative syntax in controle structure within html files , otherwise, throw error or warning if alternative syntax rules not been respected in whatever instruction .
Below is an example of alternative syntax that should be established:
<?php if (true === $var) : ?>
<h3>Hello world</h3>
<?php else : ?>
<h3>Hello</h3>
<?php endif; ?>
Is there a phpcs sniff which deals with such requirements ? if not , how can we make a customized sniff to check for alternative syntax in html files ?

Not able to print value using echo

I am using Run Script in Xcode, I am facing some problem
I am declaring variable like below
URL1 = "/Users/UserName/Desktop/Folder/13072012/libRestKit.a"
And below i am printing it echo "Test $URL1 Test" I tried like this also "Test ${URL1} Test"
I am not able to print the URL1 value, below i use this
echo "Hello, world!"
and i am able to print this, I am new to scripting What is the problem can any one help me out
Try without spaces:
URL1="/Users/UserName/Desktop/Folder/13072012/libRestKit.a"
Putting spaces around = is a common error when using bash and other shells of the same family.

jHtmlArea href link corrupted on echo?

I have a jHtmlArea as a text input for a web displayed output unfortunately any images or links that are put into the text area are corrupted with a \ before the quotation mark, when echo'd on the output page.
Is there ant way to code this differently?
Input
$text = preg_replace("/\r\n/", '', $_POST[$textIndex]) . PHP_EOL;
echo is
<?php if ($text) : // text ?>
<div><?php echo $text; ?></div>
Found the answer. It's really simple. Just change output to echo stripslashes($text)

Why do quotes turn into funny characters when submitted in an HTML form?

I have an HTML form, and some users are copy/pasting text from MS Word. When there are single quotes or double quotes, they get translated into funny characters like:
'€™ and ’
The database column is collation utf8_general_ci.
How do I get the appropriate characters to show up?
Edit:
Problem solved. Here's how I fixed it:
Ran mysql_query("SET NAMES 'utf8'"); before adding/retreiving from the database. (thanks to Donal's comment below).
And somewhat odd, the php function urlencode($text) was applied when displaying, so that had to be removed.
I also made sure that the headers for the page and the ajax request/response were all utf8.
This looks like a classic case of unicode (UTF-8 most likely) characters being interpreted as iso-8859-1. There are a couple places along the way where the characters can get corrupted. First, the client's browser has to send the data. It might corrupt the data if it can't convert the characters properly to the page's character encoding. Then the server reads the data and decodes the bytes into characters. If the client and server disagree about the encoding used then the characters will be corrupted. Then the data is stored in the database; again there is potential for corruption. Finally, when the data is written on the page (for display to the browser) the browser may misinterpret the bytes if the page doesn't adequately indicate it's encoding.
You need to ensure that you are using UTF-8 throughout. The default for web pages is iso-8859-1, so your web pages should be served with the Content-Type header or the meta tag
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
(make sure you really are serving the text in that encoding).
By using UTF-8 along all parts of the process you will avoid problems with all working web browsers and databases.
Check the encoding that the page uses. Encode it using UTF-8 as well, and add a meta tag describing the encoding:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
We have a PHP function that tries to clean up the mess with smart quotes. It's a bit of a mess, since it's grown a bit organically as cases popped up during prototype development. It may be of some help, though:
function convert_smart_quotes($string) {
$search = array(chr(0xe2) . chr(0x80) . chr(0x98),
chr(0xe2) . chr(0x80) . chr(0x99),
chr(0xe2) . chr(0x80) . chr(0x9c),
chr(0xe2) . chr(0x80) . chr(0x9d),
chr(0xe2) . chr(0x80) . chr(0x93),
chr(0xe2) . chr(0x80) . chr(0x94),
chr(226) . chr(128) . chr(153),
'’','“','â€<9d>','â€"',' ');
$replace = array("'","'",'"','"',' - ',' - ',"'","'",'"','"',' - ',' ');
return str_replace($search, $replace, $string);
}

Resources