Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Special Characters Back to ASCII or Readable

Status
Not open for further replies.

fox12

Programmer
Jan 18, 2005
62
0
0
US
I have some special chars like hyphen, quotes etc in a string. They are stored in MySql database. When I queried the record directly from the db, it appears that the records is fine by showing a hyphen (-) or quotes (either single or double). However, when I read it from the database, and write it to a text file, the hyphen appears as "<96>" without quotes. e.g.:

Normal: Topic Title - Here is a brief description
Abnormal: Topic Title <96> Here is a brief description

How can I convert it back to a hyphen? Thanks so much in advance.
 
Thanks for your answer. But I don't think my server has "recode" installed. I tried that, but it did not work. I also try "iconv" to convert it from iso-8859-1 to utf-8 and print it to a web page. This time, it shows as a strange sign.

Since the previous developer left my company, no one else know how it was ENCODED before data was entered into the database. Hence, I don't know how to DECODED them either.

what may be the possible way to convert a hyphen "-" to "<96>"? When I view it in a sql query, the hyphen show properly.
 
You could use str_replace, to change all occurrences of the "<96>" string into a real hyphen. Though I still can't figure out how it was encoded to make a hyphen <96> its defintely not ASCII as a hyphen would be 45 not 96 in ASCII.

Anyway:
Code:
$newstring=str_replace("<96>","-",$mystring);

Is there something done to the query output before being displayed? Perhaps a function or something that takes the query results and does something to them?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Vacunita,

Thank you very much. I did a str_replace, and it did write to a text file with a hyphen properly. However, I still have a problem in displaying it properly in a web browser.

I use a simple query like "select * from myTable where ID = ?" to grab the data. But I have a specified <div> in the web page, and update the content of the <div> by calling a php script via Ajax.
....
ob_start();

// connect to db and retrieve data here
...

// write to a log file
$str = ob_get_clean();
$str = preg_replace("<96>", "-", $x);
if ($s != null ) {
$logFile = "/tmp/custOrder.log";
$fh = fopen($logFile, "a+");
fwrite($fh, $str);
fclose($fh);
}
// so far so good

//to get an instance of the target page
$targetPage = (object)getPageInstance();

******** Problem Start below ********
** not sure which line **
** **********************************
//to update the value in the specified <div>
$targetPage->myDiv = $str;
echo json_encode($targetPage);

**** all the codes above are located in one php script ****
**** and called by ajax; and works fine for other ****
**** records without a hyphen or a special chars ****





 
Nothing seems amiss in your code.

unless the instantiation of the newpage is using a different $str variable than the one you ran through the preg_replace function, it should work.


By the way, since you aren't using complex patterns for the replacement, you should use str_replace() instead of preg_replace(). as it is faster and less resource intensive. Even the PHP manual suggests using str_replace instead of preg-replace.

That however should not have any effect on your output.

Make sure the $str variable being used to pass the contents to the instance of the page is the same one that was run through the replacement function.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I am sure I am using the right instance of the page. If I take off the "description" field value which contains the hyphen, it will work fine.

It now seems an Ajax issue now. I added the exit(0) before echo json_encode($targetPage), and the page popped up a javascript saying that it is missing a right parenthesis ")".
 
You should check your page contents, seems like you are missing something in the JS/AJAX part of the generation.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
good idea. I'll check that, and get back. Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top