0

I try that URL safe with following function, but it don't work for Arabic language. How can fixed it?

DEMO Here

<?php
    function stringURLSafe($string)
{
    // remove any '-' from the string since they will be used as concatenaters
    $str = str_replace('-', ' ', $string);


    // Trim white spaces at beginning and end of alias and make lowercase
    $str = trim(strtolower($str));

    // Remove any duplicate whitespace, and ensure all characters are alphanumeric
    $str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);

    // Trim dashes at beginning and end of alias
    $str = trim($str, '-');

    return $str;
}
echo stringURLSafe('موقع إخباري: مرتبط بقناة {العربية} يقدم (على) مدار');

I want in output as:

موقع-إخباري-مرتبط-بقناة-العربية-يقدم-على-مدار
hakre
  • 193,403
  • 52
  • 435
  • 836
Taylor Gomez
  • 320
  • 1
  • 5
  • 22
  • 5
    That's not a valid URL, though. You'd have to `urlencode()` the string. [Unicode characters in URLs](http://stackoverflow.com/q/2742852) – Pekka Jan 14 '13 at 08:56

1 Answers1

0

Change your regexp to this one:
$str = preg_replace('/(\s|[^\p{Arabic}\w0-9\-])+/u', '-', $str);
Here result:
موقع-إخباري-مرتبط-بقناة-العربية-يقدم-على-مدار

pstr
  • 384
  • 1
  • 2
  • 6