-1

I have two PHP files and when I fetch data from database on these pages and display them using print_r both the pages display content in different way.

Actually I have some HTML special characters in my content with I have escaped using htmlspecialchars before string in MySQL database.

when I fetch them display it on page1.php it is displayed as

My ‘pop’ to display

but on page2.php it is displayed as

My ‘pop’ to display

I also want to mention that page1.php is my webpage and I have print_r just to check the issue on page2.php where page2.php is a api page where I was using echo json_encode to write data but to check the issue I have replaced it with print_r

Nikhil Gaur
  • 1,280
  • 3
  • 19
  • 40
  • i guess its utf8 format issue so before you run json_encode function use utf8_encode then run json_code – Manthan Dave Nov 07 '16 at 06:13
  • 2
    "I have escaped using `htmlspecialchars` before string in MySQL database." Well, there's your problem. **Don't do this**. You should be inserting your data with SQL escaping, not HTML escaping. HTML escaping is to be used when you display things on a page and only then. – tadman Nov 07 '16 at 06:34
  • I tried but utf8_encode doesn't work in this case – Nikhil Gaur Nov 07 '16 at 06:44
  • @tadman I removed htmlspecialchars but still it is automatically converted to ‘pop’ – Nikhil Gaur Nov 07 '16 at 06:54
  • is it Collation issue of database column? mine is utf8_general_ci – Nikhil Gaur Nov 07 '16 at 06:55

2 Answers2

0

For Display use this code.

$message = "My ‘pop’ to display";

echo htmlspecialchars($messge, ENT_QUOTES, 'UTF-8');
Rajkeshar
  • 75
  • 10
  • I am not using htmlspecialchars now but still my text is automatically converted to My ‘pop’ to display. is it database Collation issue? mine is utf8_general_ci. – Nikhil Gaur Nov 07 '16 at 06:57
  • Yes it's database collation issue. when you insert data in database that time skip html character using mysql_real_escape_string function. – Rajkeshar Nov 07 '16 at 07:03
  • so how can I correct that? do I have to use some other collation? – Nikhil Gaur Nov 07 '16 at 07:10
0

Finally I got the solution. Because my data in database is already manipulated using htmlspecialchars and even after removing htmlspecialchars I was not able to correct the data to I found this solution to convert all html encoded characters back to original

Here is the simple code for doing this

$input = "This is the ‘text’ I fetched";
$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);
echo $output;

Referance: http://www.codewithasp.net/2016/11/html-entity-decode-to-original-php.html

UPDATED

Alternatively we can also use html_entity_decode to do the same which I found after tadman's comment.

Nikhil Gaur
  • 1,280
  • 3
  • 19
  • 40
  • Don't encourage people to write their own junk when there's a [built-in function `html_entity_decode` that does this already](http://php.net/manual/en/function.html-entity-decode.php). Step 1 is check the documentation, not start coding. – tadman Nov 07 '16 at 15:31
  • I think this piece of code is also on same documentation link which yo shared. I just used what I found in my search. Anyway thanks for sharing this solution. I will update this in my answer above. – Nikhil Gaur Nov 09 '16 at 07:26