Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    replace some corresponding letters from a string

    Hi;

    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
    200.

    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.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    You could use something like this - I would of filled in the other conversions, but.. I don't know how it works.

    PHP Code:
    <?php
    $string 
    'fromlatter="asdfghjkl.."';

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

    $search  = array(
                       
    'a',
                       
    's',
                       
    'd',
                       
    'f'
                    
    );



    $replace = array(
                       
    'o',
                       
    'l',
                       
    'i',
                       
    'f'
                    
    );

    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.

    tolatter="asd"fghjkl.."

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

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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.
    <?php

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

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    52
    Thanks
    4
    Thanked 6 Times in 6 Posts
    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:

    PHP Code:
    $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:

    PHP Code:
    $my_string 'teststring';
    $from_letter 'asdfer';
    $to_letter 'qwerzx';

    $from_array preg_split'//'$from_letter );
    $from_array array_slice$from_array1sizeof$from_array ) - );

    $to_array preg_split'//'$to_letter );
    $to_array array_slice$to_array1sizeof$to_array ) - );

    $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?

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    52
    Thanks
    4
    Thanked 6 Times in 6 Posts
    Obviously there are plenty of ways to turn a string into an array. You could also do this:

    PHP Code:
    $from_string 'asdfer';
    $from_array = array();

    for ( 
    $i=$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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •