SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replace dodgy chars

    Hi.
    I've got this php code
    Code PHP:
    protected function sanatizeInputValue($value)
      {
        $dodgychars = "#[|\!\"'\$£%&\/()=?^<>\*\+\-]#";
        $filterValue = preg_replace($dodgychars,"-",$value); 
        return substr($filterValue,0,255);
      }

    How can I tanslate this in js code
    I tried but with very poor result
    can you show me the way, please ?

    Bye

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    var toReplace = {
        '|': '-',
        '!': '-',
        '"': '-',
        '£': '-',
        '%': '-',
        '&': '-',
        '/': '-',
        '(': '-',
        ')': '-',
        '=': '-',
        '?': '-',
        '^': '-',
        '*': '-',
        '°': '-',
        '*': '-',
        '§': '-',
        '@': '-',
        '#': '-',
        '_': '-',
        ';': '-',
        ':': '-',
        '!': '-',
        '.': '-',
        '<': '-',
        '<': '-',
    };
    var string = '<This ?^*ù is a test'.replace(/./g, function (match) {
        return toReplace[match] || match;
    });
    alert(string);



    I'm lucky I found it in my repository
    I don't remember the really author

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    function sanatizeInputValue(value) {
        var dodgyChars = /[|!"'$£%&\/()=?^<>*+-]/g;
        return value.replace(dodgyChars, '-').substring(0, 255);
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JimmyP View Post
    Code JavaScript:
    function sanatizeInputValue(value) {
        var dodgyChars = /[|!"'$」%&\/()=?^<>*+-]/g;
        return value.replace(dodgyChars, '-').substring(0, 255);
    }
    Thanks for the help
    It was my first attempt but
    it didn't work may be for the lack
    of the modifier

    Bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •