0

I have a string (foreign language) and I need to convert to htmlentities.
I'm runing a php script from my terminal on linux Ubuntu.

I need this:

$str = "Ettől a pillanattól kezdve,"

To become something like this:

EttЗl a pillanattßl kezdve,

$str = "Ettől a pillanattól kezdve,";
$strEncoded = htmlentities($str, ENT_QUOTES, "UTF-8");  

$cmd = $pdo->prepare("UPDATE table SET field = :a");
$cmd->bindValue(":a", $strEncoded);  

$cmd->execute();

Database/Table Information:

  • Charset: utf8
  • Collation: utf8_general_ci

It is not saving as expected. Obs: I know it's not the best practice to use htmlentities to save into database, but I need to do it this way.

Example 2:

$a = "Quantità totale delle";
$b = html_entity_decode($a);
echo $a; //output: Quantità totale delle
echo $b; //output: Quantità totale delle (Need the reverse)
echo htmlspecialchars($b, ENT_QUOTES, 'UTF-8') . "\n"; //output: Quantità totale delle (didn't convert the special character to `à`  

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90

1 Answers1

1

To match the question, you have to rebuild the entity yourself using the dec value. This will works with strings like you specified:

<?php 
$str = str_split("Ettől a pillanattól kezdve,");

foreach ($str as $k => $v){
 echo "&#".ord($v).";";  
} 
// &#69;&#116;&#116;&#197;&#145;&#108;&#32;&#97;&#32;&#112;&#105;&#108;&#108;&#97;&#110;&#97;&#116;&#116;&#195;&#179;&#108;&#32;&#107;&#101;&#122;&#100;&#118;&#101;&#44;

But this won't work for chars above 255.

https://www.php.net/manual/en/function.ord.php

Interprets the binary value of the first byte of string as an unsigned integer between 0 and 255. If the string is in a single-byte encoding, such as ASCII, ISO-8859, or Windows 1252, this is equivalent to returning the position of a character in the character set's mapping table. However, note that this function is not aware of any string encoding, and in particular will never identify a Unicode code point in a multi-byte encoding such as UTF-8 or UTF-16.

NVRM
  • 11,480
  • 1
  • 88
  • 87