Jump to content

Replace special characters


prova17

Recommended Posts

Good evening!

 

I'm new in the forum and programming php!

I'm trying to create an application with jqmobile.

 

From the following I obtain results from db mysql but I obtain value "null" when there are special characters like ò, à, è, ù ...

 

Is it possible to replace the special characters?

 

THANKS!!!

<?php
include 'config.php';

$sql = "SELECT * FROM table_books WHERE info5 = 'thriller' ORDER BY id DESC";

try {
	$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);	
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$stmt = $dbh->query($sql);  
	$employees = $stmt->fetchAll(PDO::FETCH_OBJ);
	$dbh = null;
	echo '{"items":'. json_encode($employees) .'}'; 
} catch(PDOException $e) {
	echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

?>
Link to comment
Share on other sites

In cases where the database charset is different from utf8 you should always encode the text before sending it through ajax, this is also the case with JQM as its no way to override utf8 charset unless you edit the JQM source

 

Replacing special chars is probably not your best option

// you have some control in this example, no special characters as we can avoid them
$js_return['status'] = 'saved';

// this however might contain special characters depending of db charset etc, convert it to utf8 before sending it
$js_return['text'] = utf8_encode($db_data); 

echo json_encode($js_return);
Link to comment
Share on other sites

Thanks for help!

 

I tryed to insert the utf8_encode but I obtain the result "Undefined".


try {
	$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);	
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$stmt = $dbh->query($sql);  
	$employees = $stmt->fetchAll(PDO::FETCH_OBJ);
	$dbh = null;
	echo '{"items":'.  json_encode(utf8_encode($employees)) .'}'; 
} catch(PDOException $e) {
	echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

If you need there is the .js file.

    var serviceURL = "/services/";

    var employees;

    $(document).ready (function() {
    getEmployeeList();
    });

    $(document).delegate('#employeeListPage','pageshow', function(event) {
         getEmployeeList();
    });


    $('#employeeListPage').bind('pageinit', function(event) {
       getEmployeeList();
    });

    function getEmployeeList() {
       $.getJSON(serviceURL + 'getemployees.php', function(data) {
          $('#employeeList li').remove();
          employees = data.items;
          $.each(employees, function(index, employee) {
             $('#employeeList').append('<li><a href="employeedetails.html?id=' + employee.id + '">' +
                   '<img src="img/' + employee.img + '" width="80px" height="80px" />' +
                   '<h4>' + employee.title + '</h4>' +
                   '<p>' + employee.info1 + '</p>' +
                   '</a></li>');
          });
          $('#employeeList').listview('refresh');
       });
    }

Somebody could help me?

Thanks!

Link to comment
Share on other sites

I found a solution but I don't if it is the best.

I replace the special characters by SELECT REPLACE directly on the query.

 

$sql = "SELECT REPLACE(title, 'ò', '&ograve') as title, id, description, img, info1 FROM table_books WHERE info5 = 'thriller' ORDER BY id DESC";

 

Is it a good solution or not?

 

thank you!

Link to comment
Share on other sites

I found a solution ;)

 

I added --> array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'") and now it works :D

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));   

Now I have only a problem...When I see the description of the review book I see all the HTML tags (<p style="text-align: justify;">Trovarsi a leggere un libro noir scritto....) How can I avoid it?

 

Really thanks for all the people helped me!!

Link to comment
Share on other sites

Use character setting instead a set names sql command:

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuser, $dbpass);

 

.When I see the description of the review book I see all the HTML tags (<p style="text-align: justify;">Trovarsi a leggere un libro noir scritto....) How can I avoid it?

 

Why do you want to store html markup inside DB and latter on you want to avoid of dispalying them? 

Link to comment
Share on other sites

You shouldnt use json to transport html formatted content as it will most lightly break (as it does right now in your example - its json that breaks it), and if you are sending content to JQM you should send pure data and format it in JQM for best result. JQM can be a real pain to work with even if you try to do it according to standars, especially when moving outside utf8 (as some of us have to).

 

When working with mobile content you also want to send as little amount of data as possible between server and mobile unit for best possible performance and smallest data size.

 

This alltogether would save you from the trouble you are experiencing at this stage.

 

So if this is a new db/content setup you should reconcider storing pure data in db, if not you probably need to clean away any html before sending it through json_encode.

 

Your alternative is to drop json and send it as html through ajax.

 

Your best option is probably to run the query with default encoding and adapt the data with php before sending it.

Edited by alpine
Link to comment
Share on other sites

Thanks for the advice but I tryed as you said and it doesn't work.

 

I have a question, is it incorrect use this code?

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

About the HTML markup in the DB don't ask me why but I'm getting data from a CMS and save the HTML markup in the description.

I solved the problem in the js file using "html" insted "text".

function displayEmployee(data) {
	var employee = data.item;
	console.log(employee);
	$('#employeePic').attr('src', 'img/' + employee.img);
	$('#employeeTitle').text(employee.title);
	$('#description').html(employee.description);

Thanks!

Link to comment
Share on other sites

Thanks alpine for the advice, unfortunately I'm not really an expert and I'm trying to learn as I can by forum and internet.

Hope what I did isn't included in "best practise" for an application using JQM and phonegap.

I installed in a mobile device and it work perfectly, I'm just scared it will be slow in the future when there will be more data in the db, unfortunately I can't change the db settings.

 

Thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.