1

I am have the following code:

For User

@john -> <a href="homeurl/profile/john">@john</a>

For Hashtag

#hello -> <a href="homeurl/hashtag/hello">#hello</a>

But in the PHP hashtags have Turkish letters (on the title). So hastags slug sanitized from (title). And Turkish letters replace to basic latin letters.

Turkish Letters: ığüşöç İĞÜŞÖÇ

(function($) {
  $.fn.autolink_regex_map = new Array(
    {
      're': /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, // URLs
      'replace': '<a rel="nofollow" href="$1">$1</a>'
    },
    {
      're': /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=-]*>))/g, // Email addresses
      'replace': '<a rel="nofollow" href="mailto:$1">$1</a>'
    },
    {
      're': /(^|\s)@(\w+)/g, // @-mentions
      'replace': '$1<a rel="author" href="' + homeurl + '/profile/$2">@$2</a>'
    },
    {
      're': /(^|\s)#(\w+)/g, // Hashtags
      'replace': '$1<a rel="search" href="' + homeurl + '/hashtag/$2">#$2</a>'
    }
  );

  $.fn.autolink = function() {
    return this.each(function() {
      var $this = $(this);

      $.each($.fn.autolink_regex_map, function(i, obj) {
        $this.html($this.html().replace(obj.re, obj.replace));
      });
    });
  }
})(jQuery);

This image result on following code:

#değilmi -> <a href="homeurl/hashtag/de">#de</a>ğilmi

How to detect the Turkish letters and how to replace latin version for the url e.g.

<a href="homeurl/hashtag/degilmi">#değilmi</a>

2 Answers2

1

For the slug I am using this script:

var string_to_slug = function (str)
{
    str = str.replace(/^\s+|\s+$/g, ''); // trim
    str = str.toLowerCase();

    // remove accents, swap ñ for n, etc
    var from = "àáäâèéëêìíïîıòóöôùúüûñçşğ·/_,:;";
    var to   = "aaaaeeeeiiiiioooouuuuncsg------";

    for (var i=0, l=from.length ; i<l ; i++)
    {
        str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
    }

    str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
        .replace(/\s+/g, '-') // collapse whitespace and replace by -
        .replace(/-+/g, '-'); // collapse dashes

    return str;
}

Maybe any one need it.

0

You can follow the footsteps of Efficiently replace all accented characters in a string? and translate the Turkish characters to ASCII letters like that:

var makeSortString = (function() {
  var translate_re = /[ığüşöçİĞÜŞÖÇ]/g;
  var translate = {
    "ı": "i", "ğ": "g", "ü": "u", "ş": "s", "ö": "o", "ç": "c",
    "İ": "I", "Ğ": "G", "Ü": "U", "Ş": "S", "Ö": "o", "Ç": "C",
  };
  return function(s) {
    return ( s.replace(translate_re, function(match) { 
      return translate[match]; 
    }) );
  }
})();

However, if you can make use of ECMAScript 6, it introduces the method String.prototype.normalize() which takes care of Unicode normalization for you. You can use it with unorm a polyfill for common JS.

console.log('ığüşöçİĞÜŞÖÇ'.normalize('NFKD'));
wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Thank you @wp78de your script working. But get now slug problem. I was resolve it this regex: `[a-z0-9_ğüşöçıİĞÜŞÖÇ]` –  Apr 28 '18 at 22:57