skip to Main Content

Is there any equivalent in javascript/nodejs for PHP’s mb_convert_case() function?

2

Answers


  1. I think this is what you were looking for:

    function mb_convert_case(str, mode) {
      switch (mode) {
        case 'MB_CASE_UPPER':
          return str.toLocaleUpperCase();
        case 'MB_CASE_LOWER':
          return str.toLocaleLowerCase();
        case 'MB_CASE_TITLE':
          return str.replace(/bw/g, function (char) {
            return char.toLocaleUpperCase();
          });
        default:
          return str;
      }
    }
    
    // Example usage:
    const inputString = "hÉllO wOrLD";
    console.log(mb_convert_case(inputString, 'MB_CASE_UPPER')); // Output: HÉLLO WORLD
    console.log(mb_convert_case(inputString, 'MB_CASE_LOWER')); // Output: héllO world
    console.log(mb_convert_case(inputString, 'MB_CASE_TITLE')); // Output: Héllo World
    Login or Signup to reply.
  2. There’s no javascript alternatives for this function. But in javascript you can use toUpperCase/toLocaleUpperCase and toLowerCase/toLocaleLowerCase for convert string to case that you need.
    For more complicated cases you can look at some third party libraries. For example lodash have some useful helpers for work with string case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search