0

In my table the table column name is description that store the book description, when I fetches the description from database the string Unicode \u00e2\u20ac\u2122 also in my string I want to remove these value from the string. Bellow is my php code

case 'bookdetails': {
                $query = "SELECT * FROM book";
                $result = mysql_query($query);
                $num = mysql_num_rows($result);
                while($rows = mysql_fetch_assoc($result)) {
                    $response["Book"]["Id"] = $rows['id'];
                    $response["Book"]["BookName"] = $rows['bookName'];

                    $response["Book"]["Description"] = strip_tags($rows["description"]);
                }
                if($num > 0) {
                    $response["success"] = 1;
                } else {
                    $response["error"] = 1;
                    $response["Error_msg"] = "No record founds.";
                }
                echo json_encode($response);
            break;
            }

Output: Description : Afterwards he would pop them into the tumble drier \u00e2\u20ac\u201c etc....

I want to remove \u00e2\u20ac\u201c these from my output string.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Krishna Kumar Yadav
  • 313
  • 1
  • 5
  • 16
  • 3
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Jan 13 '15 at 13:54

1 Answers1

1

These are Unicode code points. They are probably significant content in your database.

You could try this:

  $response["Book"]["Description"] = strip_tags(htmlentities($rows["description"]));

But, if you're using the JSON to send this information to a browser, it's very likely the browser will do the right thing. For example, \u20ac is € and \201c is “ (left double quote). The htmlentities() api call will turn the unicode codepoints into their entity equivalent, which makes them slightly easier to read.

  \u20ac   €
  \201c    “

If you prefer to convert the text strings to the 8-bit latin1 (iso8859-1) character set, you can use this kind of clause in your SELECT statement.

SELECT CONVERT(description USING latin1) description,
       CONVERT(title USING latin1) title

If you want old-timey 7-bit ASCII, you can do this

SELECT CONVERT(description USING ascii) description ...

If the codepoints in your data can be represented in the target character set, they'll be converted. Otherwise they'll be substituted with ? (0x3f), a replacement character.

If you need to search your database contents, that's another question.

O. Jones
  • 103,626
  • 17
  • 118
  • 172