What should be used instead of "|" as separator in php script?

  • Hi,

    In my game i POST a Dictionary.AsJson through AJAX that then gets saved on a mysql database. When i retrieve that data i have to set a separator in php to get the list in form of an array, the code line look like this :

    echo $rows['Name']. "|" . $rows['DictJson']. "|" . $rows['OtherData']. "|"

    We generally use "|" as separator but in this case that causes a problem because dictionary is saved like "a|b|c|X|y|Z" and when i use tokenat(AJAX.LastData, 1, "|") it doesn't give the right json as it detects a "|" in between dictionaryjson data.

    I used ">|<" instead of "|" to solve this but that also causes issue some times, so i wanna know if there is a better separator that will work with all kinds of data correctly?

  • You need to properly escape the separator if it occurs inside data strings. E.g. if the data string includes "|", replace it with a different character. Then of course you have the problem that if the replaced character appears, you have to replace that with something else.

    If you just use a standard format like JSON that already supports all of this so you don't have to worry about it. It's why you should use existing data formats instead of inventing your own, and then having to solve all the same problems data formats have to solve.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Sorry, i didn't realized i was using format "text" instead of "Json". Thanks for the info.

    Can you please tell me what format(data type) i should use for posting 'URLEncode(BinaryData.GetBase64)'string?

  • Also changing the format to JSON in mysql did not changed anything as the type remains "longtext" (i googled it and turns out longtext is alias to Json https://stackoverflow.com/questions/50081310/mysql-not-allowing-json-data-type) and before it was set to longtext too.

    I have no problem using standard "|" for separating values but that sometimes gives incorrect data due to dictionary being like |x|y| as i said before.

  • you will probably want to use a field type of BLOB in your database for the base64 string - but I haven't tried that to see if there are things to watch out for.

    if you want to store the data in your MySQL database using the field names you show above, then you would have to use json_encode and json_decode in your php scripts to send or receive data from Construct3.

    (you would build an array in php from the data you read from the database and then use that with json_encode to get a json string to send to C3, when you receive data from C3, json_decode will give you an array that you loop through to build queries to update your database.)

    or do it manually like you are tying but with safe deliminators. I do a lot of database work with Construct and MySQL - I use a delimiter that can't be typed on the keyboard (unless you hold down the Alt key and type 0166). I use ¦ for a field deliminator and ¶ for a record deliminator. (alt-0166, and alt-0182).

    php doesn't like those characters typed in that way so I set variables like this:

    $f="%c2%a6"; $r="%c2%b6"; // field and record delimiters

    and then use them like this:

    echo $rows['Name']. $f . $rows['DictJson']. $f . $rows['OtherData']. $r;

  • Thanks!

    I made it work by using text for base64 string and Json(that again sets automatically to longtext) for json string, and used |~| as separator and everything is working fine.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)