What is the best way to encrypt a string in PHP using a specific character mapping?

One way to encrypt a string in PHP using a specific character mapping is to create a custom encryption algorithm that substitutes each character in the input string with a corresponding character from the mapping. This can be achieved by creating an associative array where the keys are the characters in the input string and the values are the corresponding characters from the mapping. Then, iterate over each character in the input string and replace it with the mapped character.

function encryptString($input, $mapping) {
    $encryptedString = '';
    
    $charMapping = array();
    foreach(range('a', 'z') as $index => $char) {
        $charMapping[$char] = $mapping[$index];
    }
    
    for($i = 0; $i < strlen($input); $i++) {
        $char = $input[$i];
        $encryptedString .= isset($charMapping[$char]) ? $charMapping[$char] : $char;
    }
    
    return $encryptedString;
}

$input = "hello";
$mapping = array('x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w');

echo encryptString($input, $mapping);