Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9303877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:35:07+00:00 2026-06-18T23:35:07+00:00

I’ve this javascript function that convert any string into the perfect slug (in my

  • 0

I’ve this javascript function that convert any string into the perfect slug (in my opinion).

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

  var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
  var to   = "aaaaeeeeiiiioooouuuunc------";
  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, '').replace(/\s+/g, '_').replace(/-+/g, '-'); 
  return str;
}

I need to convert it to PHP, I’ve tried and the result is:

function slugGenerator($str) {
  $str = preg_replace('/^\s+|\s+$/g', '', $str);
  $str = strtolower($str);

  $from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
  $to   = "aaaaeeeeiiiioooouuuunc------";

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

  $str = preg_replace('/[^a-z0-9 -]/g', '', $str)
  $str = preg_replace('/\s+/g', '_', $str)

  $str = preg_replace('/-+/g', '-', $str); 
  return $str;
}

I’ve problems with this for loop:

for ($i = 0, $l = strlen($from); $i<$l ; $i++) {

  // This string
  $str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}

I’ve not idea on how convert it to PHP, someone could try to convert it?

SOLUTION:
Add the strtr_unicode function and use this script:

function slugGenerator($str) {

    $str = preg_replace('/^\s+|\s+$/', '', $str);
    $str = strtolower($str);

    $from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
    $to   = "aaaaeeeeiiiioooouuuunc------";
    $str = strtr_unicode($str, $from, $to);

    $str = preg_replace(
      array("~[^a-z0-9 -]~i", "~\s+~", "~-+~"),
      array("", "_", "-"),
      $str
    );

    return $str;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:35:08+00:00Added an answer on June 18, 2026 at 11:35 pm

    Both strtr and str_split won’t work for you cos your code contains unicode chars. There are some useful stuff if you like to use.

    str_split_unicode: https://github.com/qeremy/unicode-tools.php/blob/master/unicode-tools.php#L145
    strtr_unicode: https://github.com/qeremy/unicode-tools.php/blob/master/unicode-tools.php#L223

    Test:

    echo strtr_unicode("Hëëëy, hôw ärê yôü!", $from, $to);
    // outs: Heeey- how are you!
    

    After that, you can use arrays as param for preg_replace;

    $from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
    $to   = "aaaaeeeeiiiioooouuuunc------";
    $str = strtr_unicode("Hëëëy, hôw ärê yôü!", $from, $to);
    echo $str ."\n";
    // according to this line: 
    // str = str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '_').replace(/-+/g, '-');
    $str = preg_replace(
        array("~[^a-z0-9 -]~i", "~\s+~", "~-+~"),
        array("-", "_", "-"),
        $str
    );
    echo $str;
    

    Outs;

    Heeey- how are you!
    Heeey-_how_are_you-
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.