Jump to content

PHP function to escape necessary characters


Molson31

Recommended Posts

Hello. My client and I are trying to figure out a strange issue with exporting blobs. We are trying to sync a WAMP based databse and local MySQL database. The problem is with blob columns. 

The issue: 

When exporting a blob, the local MySQL dump command inserts an escape character "\" before characters such as NUL, TAB and LF, and I am assuming a lot more. My PHP export function (opens a file, SELECT * FROM tbl, writes to the file) does not do this. 

The main issue is that my client cannot import my PHP dump files when there are blobs, which I suppose is because they are missing these escape characters. He can however re-import his own MySQL dumps. 

I know I could remedy by using PHP string replace functions to find each character and insert a '\' before it, but I don't know which characters will need to be escaped. I also feel like there is a more simple solution. For example this is what I have and it worked for a tiny 1 pixel image, but not for something complex:

$val = str_replace(chr('NUL'), '\0', $val);							
$val = str_replace(chr(0x9), chr(0x5c).chr(0x9), $val);	
$val = str_replace(chr(0xa), chr(0x5c).chr(0xa), $val);	

Thanks

Link to comment
Share on other sites

It's a 'blob' but you seem to be treating/thinking of the contents as a string. While a string can hold non-ascii characters, they will be a problem when trying to print them. They should not be problem when saving the string to a file.

 

When I don't know how my data will affect its container, I base64_encode() it first.

 

But I am not fully understanding what you are doing where you feel the need to escape certain bytes (control codes).

Link to comment
Share on other sites

It's a 'blob' but you seem to be treating/thinking of the contents as a string. While a string can hold non-ascii characters, they will be a problem when trying to print them. They should not be problem when saving the string to a file.

 

When I don't know how my data will affect its container, I base64_encode() it first.

 

But I am not fully understanding what you are doing where you feel the need to escape certain bytes (control codes).

 

There are two database dumps in question

 

A) Local MySQL

- When it exports blobs, it inserts escape character "\" before many characters such as LF, TAB and NUL

 

B) Webhost MySQL

- Using a custom PHP function, when it exports a blob, it does not have these escape characters

 

I am trying to understand what this means and how to make either one like the either. At this point I am thinking the best way is to insert escape characters in the Webhost dump, so I guess the next question is which ones. I am wondering as well if this is something built-in somewhere that someone may know of.

Edited by Molson31
Link to comment
Share on other sites

I can't do something like that. This is on typical business web hosting.

Ask your host if mysqldump is already available for you to use. They may have it installed already and give you the information necessary to use it.

 

If you're absolutely stuck using PHP to dump the data though, then I would hex encode the blob columns. Then you don't have to worry about what characters need to be escaped vs which are ok as is. Just encode everything.

  • Like 1
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.