Help escape special characters php

Asked

Viewed 311 times

0

I’m having trouble saving special characters. I’m developing a system where the teacher needs to write an observation in a textarea. I am using the code below to save/update the observation field:

$.ajax({
 url: '<?php echo site_url('accountant/update_planning/'); ?>' + id + 
 '/' + encodeURI(observation) + '/' + status,
  success: function (response)
     {
       buttonSend.innerHTML = 'Salvar';
            $.notify({
                title: '<strong>Sucesso!</strong>',
                message: 'O planejamento foi atualizado com sucesso.'
            }, { type: 'success' });
        },
        error: function (response)
        {
            buttonSend.innerHTML = 'Salvar';
            $.notify({
                title: '<strong>Erro!</strong>',
                message: 'Ocorreu um erro ao atualizar o planejamento.'
            }, { type: 'danger' });
        }
    });

The problem is this: When using the encodeuri function, it does not escape (A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #)

When using the escape(Observation) function, it does not miss the accents.

So what I need is for you to escape accept the accents and the characters as (, ! ; / ? :)

How to get around this problem?

  • You can capture the data using base64encode, base64decode... have this on javascript.

3 answers

3


You used the wrong function, encondeURI is to escape in "whole" URI, example:

var uri = '/?q=[javascript] á é ? #teste a b c ó ú';
var resultado = encodeURI(uri);
console.log(resultado);

To use in "normal" strings use the encodeURIComponent thus:

url: '<?php echo site_url('accountant/update_planning/'); ?>' + id + 
'/' + encodeURIComponent(observation) + '/' + status,
  • 1

    Grateful for the explanation and help.

-1

I’ll do an example that uses base64encode/base64decode ok:

var Base64 = {


    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",


    encode: function(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },


    decode: function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0, c1 = 0, c2 = 0, c3 = 0, c = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

Do the Encounter:

var observation = Base64.encode(observation);

Would look like this:

$.ajax({
 url: '<?php echo site_url('accountant/update_planning/'); ?>' + id + 
 '/' +  observation + '/' + status,
  success: function (response)
     {
       buttonSend.innerHTML = 'Salvar';
            $.notify({
                title: '<strong>Sucesso!</strong>',
                message: 'O planejamento foi atualizado com sucesso.'
            }, { type: 'success' });
        },
        error: function (response)
        {
            buttonSend.innerHTML = 'Salvar';
            $.notify({
                title: '<strong>Erro!</strong>',
                message: 'Ocorreu um erro ao atualizar o planejamento.'
            }, { type: 'danger' });
        }
    });

On the server side I don’t know how you’re capturing it, because you didn’t add it to the question, so I’m going to pretend it was like this:

$observation = base64_decode($_POST['observation']);

-4

ola amigo may be problem at the time of creating the bank checks how this UTF-8 encoding usually and the default, if it is sending in another format that does not match the bank may occur this error. for example current browsers only send in UTF-8...

  • 2

    "current browser only send in UTF-8", how so? What is the source of this information?

  • Anderson Carlos may have expressed badly, usually get/post the browser tries to send ISO-8859-1 charset, has to have this observation to ñ have conflicts on the server side.. I didn’t understand was "When you have enough reputation, you’ll be able to leave comments on any post" if it takes how many points to help the friend in doubt? the other friend put "On the server side do not know how you are capturing, because you did not add in the question, so I will simulate that it was like" was this my question that could be of solution too!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.