Help translating to php

MJ0730

Active member
Would anyone be so kind as to convert the following js to php? I use the script and wrap text with a class to use this, but I'd like to use the proper bbcode functionality. TIA

JavaScript:
function memeify(str, base = 3){ //str is the string to memeify, base is the number we use to randomly change the casing of the letter
    str = str.split('').map(function(x){ //split the string into an array. then map
        let random = Math.floor(Math.random()*100);
    
        //If the random integer is perfectly divisible by our base
        if(random % base == 0){
              //Change our value to uppercase
            return x.toUpperCase();
        }
        else{
              //Other wise it stays lower
            return x.toLowerCase();
            }
    }).join(''); // Join the string back together
    return str; // return it
}
 
Hello,

I've tried :)

PHP:
<?php

function memeify($str, $base = 3) {
   
    for($i = 0; $i < strlen($str); $i++) {
       
        // No need for the *100 because mt_rand gives you nice integers
        $random = mt_rand();
       
        if($random % $base == 0) {
           
            $str[$i] = strtoupper($str[$i]);
           
        } else {
           
            $str[$i] = strtolower($str[$i]);
           
        }
       
    }
   
    return $str;
   
}


echo memeify('This is a meme test').'<br/>'.PHP_EOL;
// Example output:
// tHIs is A MEme test


function mb_memeify($str, $base = 3, $encoding = null) {
   
    if(is_null($encoding)) {
       
        $encoding = mb_internal_encoding();
       
    }
   
    $output = '';
    $length = mb_strlen($str, $encoding);
    for($i = 0; $i < $length; $i++) {
       
        $currentCharacter = mb_substr($str, $i, 1, $encoding);
       
        // No need for the *100 because mt_rand gives you nice integers
        $random = mt_rand();
       
        if($random % $base == 0) {
           
            $output .= mb_strtoupper($currentCharacter, $encoding);
           
        } else {
           
            $output .= mb_strtolower($currentCharacter, $encoding);
           
        }
       
    }
   
    return $output;
   
}

echo mb_memeify('This is an UTF-8 meme test. öäüß🤷‍♂️').'<br/>'.PHP_EOL;
// Example output:
// tHIS is An utf-8 MEME tesT. ÖäÜß🤷‍♂️

Documentation:
The first function memeify does pretty much what (and also how) your JS codes works. It just doesn't split the string into an array because in PHP, like in C, a string is already an array of Chars.
However. This only works for Latin characters. So if you know you'd only use English memes, this is gonna be fine.

mb_memeify supports multibyte encodings, such as UTF8. It works a little different. It cuts one multibyte character at a time, checks it, and puts it into a new variable which gets outputted eventually.

I hope that helps you.

PS: If you want to try it: phpFiddle doesn't support mb_* functions, so this will cause a parse error. But a real server should be fine.
 
Back
Top Bottom