View Full Version : replace some corresponding letters from a string

03-11-2009, 10:11 AM

I got a string(myString)which will be replaced some letter. and also i got another two of string, one is fromlatter="asdfghjkl.."
, and another one is tolatter="olik90=ky...", the two strings of the fromlatter and to latter are same length, the length may be more than

And i want to if myString has a letter a will change to o, s=>l, d=>i, f=>k .. and so on.

Could anyone tell me how to do that, please.

03-11-2009, 10:31 AM
You could use something like this - I would of filled in the other conversions, but.. I don't know how it works.

$string = 'fromlatter="asdfghjkl.."';

preg_match('#(?:from|to)latter="([^"]+)"#i', $string, $match);

$search = array(

$replace = array(

echo str_ireplace($search, $replace, $match[1]);

This will only work if the content between the two "" does not contact " as soon as it does, it'll stop at that point. e.g.


will only convert "asd", I hope this helps.

03-11-2009, 11:52 AM
Thanks, the str_ireplace() works very good for me, but i want to ask compare with strtr, which one more safety, and faster.

and also please show me how to use the regular expression, thanks

i wrote as following, always return 0 to me.

$string = 'fromlatter="asdfghjkl.."';
echo preg_match('#(?:$from|$to)="([^"]+)"#i', $string, $match);

03-11-2009, 07:32 PM
If you had a string ($my_string), and you wanted to replace some letters in it with some other letters, you might do something like this:

$my_string = 'teststring';
$find = array( 's', 'r', 'i', 'n', 'g' );
$replace = array( 'b', 'q', 'l', 'j', 'y' );
$new_string = str_replace( $find, $replace, $my_string );

Which seems to be what you want to do. The only complication is that rather than having two arrays, one containing the letters to be replaced, and the other containing the letters to replace them with, you seem to have two strings. So if you can take those two strings, $from_letter and $to_letter, and convert them to arrays, your problem would be solved. The shortest way to do that is this:

$my_string = 'teststring';
$from_letter = 'asdfer';
$to_letter = 'qwerzx';

$from_array = preg_split( '//', $from_letter );
$from_array = array_slice( $from_array, 1, sizeof( $from_array ) - 2 );

$to_array = preg_split( '//', $to_letter );
$to_array = array_slice( $to_array, 1, sizeof( $to_array ) - 2 );

$new_string = str_replace( $from_array, $to_array, $my_string );
print $new_string;
# prints 'tzwtwtxing'

This is just one way to turn a string into an array of letters. preg_split splits up strings using a regular expression. The regular expression I passed, '//', is actually a blank regular expression, and so it automatically splits the string up after every single letter. This also gives two extra values, because you end up with a blank entry at the start and end of the array. The array_splice just strips out these blank entries. Then, str_replace is ran on the original string, with the arrays created from preg_split. Make sense?

03-11-2009, 07:35 PM
Obviously there are plenty of ways to turn a string into an array. You could also do this:

$from_string = 'asdfer';
$from_array = array();

for ( $i=0 ; $i<strlen($from_string) ; ++$i )
$from_array[] = $from_string[$i];

Both methods accomplish the same thing. I just like the first one more. I'm not actually sure which might be faster.