1

I am new to PHP and character conversions so the title of my question might be missleading.

I am parsing one website and in one string, I want to parse, is a special character like this:

<tag>Hello! My name is &#382;enk!</tag>

Now this is the text I will be inserting into my database so I need &#382; converted to character 'ž' (its ASCII code).

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user3787774
  • 71
  • 1
  • 8

4 Answers4

2

Use html_entity_decode() and explicitly specify the charset:

$string = html_entity_decode($string, ENT_QUOTES, "utf-8");

for future reference: PHP string functions

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
Ajay Raturi
  • 689
  • 5
  • 22
0

Try below code.

$input = "Hello! My name is &#382";

$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);

echo $output;

hope this helps.

user247217
  • 394
  • 1
  • 9
  • Hello, no still no luck i am getting: ´╗┐Hello! My name is ž – user3787774 Dec 09 '14 at 12:11
  • There is something wrong ... because i just cant get an output i am looking for and i have tried nomerous of options from various forums before posting question here. – user3787774 Dec 09 '14 at 12:11
0

Use the below code to solve this issue

$string_to_convert="your string";

$utf8_converted_string=utf8_encode($string_to_convert);

echo $utf8_converted_string // Output the utf8 characters

John Peter
  • 21
  • 3
  • Hello, this also preduce same resoult as other possible solutions. Output is: ž ... i am starting to believe there is another problem somehwere ... i really have no idea what else to try. – user3787774 Dec 09 '14 at 12:26
0

The problem isn't in displaying the data in the browser, because I tried the following and it worked perfectly:

<?php echo '<tag>Hello! My name is &#382;enk!</tag>'; ?>

The problem isn't in saving the data in the database.

The problem is in retrieving this character from the database.

So you need to set the format to UTF-8 before querying the database:

$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");

If you're using mysqli:

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
if(mysqli_connect_errno()){
    printf("DB Connect failed: %s\n", mysqli_connect_error());
    exit();
}
// Add the UTF-8 Support
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");

// Query the database
$mysqli->query("SELECT column FROM `table` ...");
Wissam El-Kik
  • 2,469
  • 1
  • 17
  • 21
  • Also it's a good practice to assign the following "meta tag" at the top of your document: insert `` into the `` tag of your HTML document. – Wissam El-Kik Dec 09 '14 at 13:03