Les fonctions de PHP en JavaScript

24 Mars 2014 à 00:00 Programmation

Je ne publierai plus rien sur ce blog. Un nouveau blog existe maintenant. Il se nomme Hardly Smart.

Vous pourrez y retrouver des astuces plus larges sur l'informatique ainsi que mes anciens billets de blog.

Je vous souhaite une bonne lecture sur Hardly Smart.


Si comme moi les fonctions PHP vous sont plus familière que les fonctions JavaScript, je vous conseil de garder ce site en mémoire.

php.js est un site créé par Kevin van Zonneveld et ses contributeurs qui regroupe toutes (enfin un bon paquet) de fonction PHP en JavaScript. Ainsi si vous avez un site fullJS à créer et que cela fait bien longtemps que vous n'avez pas touché à ce langage vous trouverez votre bonheur sur ce site.

Si vous utilisez NodeJS pour vos projet voiçi comment l'installer :

$ mkdir test && cd $_
$ npm install phpjs
$ $EDITOR try.js

Dans la liste des fonctions les plus connues vous avez :

Et sans oublier le lien vers le GitHub du projet : https://github.com/kvz/phpjs

Source

function htmlentities(string, quote_style, charset, double_encode) {
  //  discuss at: http://phpjs.org/functions/htmlentities/
  // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  //  revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  //  revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // improved by: nobbler
  // improved by: Jack
  // improved by: Rafał Kukawski (http://blog.kukawski.pl)
  // improved by: Dj (http://phpjs.org/functions/htmlentities:425#comment_134018)
  // bugfixed by: Onno Marsman
  // bugfixed by: Brett Zamir (http://brett-zamir.me)
  //    input by: Ratheous
  //  depends on: get_html_translation_table
  //   example 1: htmlentities('Kevin & van Zonneveld');
  //   returns 1: 'Kevin & van Zonneveld'
  //   example 2: htmlentities("foo'bar","ENT_QUOTES");
  //   returns 2: 'foo'bar'

  var hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style),
    symbol = '';
  string = string == null ? '' : string + '';

  if (!hash_map) {
    return false;
  }

  if (quote_style && quote_style === 'ENT_QUOTES') {
    hash_map["'"] = ''';
  }

  if ( !! double_encode || double_encode == null) {
    for (symbol in hash_map) {
      if (hash_map.hasOwnProperty(symbol)) {
        string = string.split(symbol)
          .join(hash_map[symbol]);
      }
    }
  } else {
    string = string.replace(/([sS]*?)(&(?:#d+|#x[da-f]+|[a-zA-Z][da-z]*);|$)/g, function (ignore, text, entity) {
      for (symbol in hash_map) {
        if (hash_map.hasOwnProperty(symbol)) {
          text = text.split(symbol)
            .join(hash_map[symbol]);
        }
      }

      return text + entity;
    });
  }

  return string;
}

 

Articles liés

Les commentaires ont été désactivés.

0 commentaire