How can I add random character from [A-Za-z0-9]
/
or -
to a string every second character?
e.g.
input:
Hello_world!
output:
H3e7l2l-o2_aWmocr9l/db!s
Edit:
Here is what I've tried, however without the line below the one marked Here
that throws an error
Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in...
.
I guess it's because a fragment of $char is not an array.
After I'd added the line below Here
to "convert" the string to array another error appeared:
Uncaught TypeError: str_repeat(): Argument #1 ($string) must be of type string, array given in...
<?php
$string = "Hello_World!";
$length = strlen($string);
$string = str_split($string, 2);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/-";
//Here
$chars = (is_array($chars)) ? $chars : [$chars];
for($i = 0; $i < ($length / 2); $i++){
$char = substr(str_shuffle(str_repeat($chars, 1)), 0, 1);
$added = implode($string[$i], $char);
}
echo $string;
?>