What is the best way to exchange alpha characters for special characters? (a = !, b = ", c = £, ...)

I am working with an API that takes three text inputs as a letter and creates an image with effects.

One of the styles must have three characters, the middle character must be uppercase, the last character must be lowercase, but the first character must be a special character.

I am using PHP to change the second and third characters, which is light enough, (lcase / ucase)

But the first one is a little more complicated, since how would I do it, would it be 26 commands IF

or CASE

... I'm sure there must be a way to do this with an array?

A=! 
B="
C=# 
D=$ 
E=% 
F=& 
...

      

Is there an easy way to put both sets into an array and "swap" the values?

Thank!

+3


source to share


1 answer


You can use str_replace()

that accepts arrays. http://php.net/manual/de/function.str-replace.php



$search = array( 'A', 'B', 'C', 'D');
$replace = array( '!', '"', '#', '$');

$result = str_replace( $search, $replace, $content );

      

+4


source







All Articles