CapitanFindus
CapitanFindus

Reputation: 1526

html_entity_decode after filter_var not working as expected

I guess I'm having a problem with html_entity_decode, that probably has a simple solution which I've not found yet. This is my problem, I have this string:

L'evento dell'anno

Which when I save into my db, gets filtered with filter_var($input,FILTER_SANITIZE_STRING); and outputs this:

L'evento dell'anno

Now, when I retrieve it directly from PHP inside a page, I don't need no encoding / decoding functions, and text shows as expected, as I wrote it initially, but, when I load it from AngularJS ( version 1) $http.get() it doesn't replace chars, if I print the object values I see all those HTML entities ( I think they are, I'm not sure ).

This is my AngularJS code part:

$http.get(php_data.ajax_url, {
    params: {
        action: 'get-sostenitore',
        get_id: show_id
    }
}).then(function (response) {
    // Handle data
});

The // Handle data part only prints results, nothing more.

Didn't work, so I added this PHP part:

$decoded_vars = array_map(function ($val) {
    return html_entity_decode($val, ENT_COMPAT,'UTF-8');
}, get_object_vars($object));

Doesn't work too.
I've tried htmlentities_decode too, mb_convert_encoding($val,'UTF-8','HTML-ENTITIES'), but converts special letters too ( à, è, etc... ).

I've been searching here on StackOverflow for about 2 hours, and I've found some results, like this, this, or this (I've found another full guide to encoding / decoding here on SA, but I didn't find it again ), but none of them solved my problem.
The funny thing is that I've tried pasting my string on this online tool and it shows correctly.
Could it be an AngularJS problem? I'm becoming mad for this, but I'm sure it has a really simple solution

Update

Even if I've solved my problem, I thought it was a PHP - related issue, not JS, but I was actually wrong, so I will update the AngularJS part by showing how I was using the data I get:

JS Code

app.controller('eventController', ['$http', '$scope', '$httpParamSerializerJQLike', '$interval', 'ngDialog', function ($http, $scope, $httpParamSerializerJQLike, $interval, ngDialog) {
    var event = this;
    $http.get(php_data.ajax_url, {
        params: {
            action: 'get-event',
            get_id: show_id
        }
    }).then(function (response) {
        // Handle data
        if(response.status === 200){
            var data = response.data;
            ngDialog.open({
                className: 'ngdialog-theme-default',
                data: data,
                controller: 'dialogEventi'
                controllerAs: 'evView'
            });
        }
    });

}]);

app.controller('dialogEventi', ['$http', '$scope', '$interval', '$httpParamSerializerJQLike', function ($http, $scope, $interval, $httpParamSerializerJQLike) {
    var evView = this;
    evView.loadedItem = $scope.ngDialogData;
}]);

Popup code

<div id="show-event" class="bpopup-popup">
    <div class="popup-head">
        <h2>Visualizza evento</h2>
    </div>
    <div class="popup-body clearfix">
        <div>
            <div class="form-group" ng-show="evView.loadedItem.titolo">
                <label>Titolo:</label>
                {{evView.loadedItem.titolo}}
            </div>
            <div class="form-group" ng-show="evView.loadedItem.descrizione">
                <label>Descrizione:</label><br/>
                {{evView.loadedItem.descrizione}}
            </div>
            <div class="form-group" ng-show="evView.loadedItem.dataora">
                <label>Data:</label>
                {{evView.loadedItem.dataora}}
            </div>
            <div class="form-group" ng-show="evView.loadedItem.luogo">
                <label>Luogo:</label>
                {{evView.loadedItem.luogo}}
            </div>
            <div class="form-group" ng-show="evView.loadedItem.indirizzo">
                <label>Indirizzo:</label>
                {{evView.loadedItem.indirizzo}}
            </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 546

Answers (3)

gladiola
gladiola

Reputation: 133

Use FILTER_SANITIZE_FULL_SPECIAL_CHARS

The manual says it will do the single and double quotes. Because it will be like htmlspecialchars() but with ENT_QUOTES. Maybe that's what you need.

Upvotes: 1

CapitanFindus
CapitanFindus

Reputation: 1526

after hours of searching, I've found a solution, which is angular-sanitize, which I've found here. So, to solve my problem I've just added ngSanitize as module dependancy.

Anyway, thanks your all your answers, I've found something interesting in each one of them which I will keep in mind.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

So angular {{}} or ng-bind only present text. You can convert html entities quite simply in browser by creating dom element, inserting the string as html and retrieving it as text.

// simple helper function
function decode_entities(str){
  return angular.element('<div>').html(str).text()
}

This could be used as an angular filter or as a predecessor when you receive data

data.forEach(function(item){
     item.description  = decode_entities(item.description);
});

Upvotes: 1

Related Questions