• [deleted]

Hi

I want to use str_replace to look for a set of characters and replace them with another set.

So that all instances of individual characters are replaced with a different one.

eg

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

Would I would like to try and do is something like

// Provides: H2ll W4rld 4f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$myarray=array(("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$onlyconsonants = str_replace($vowels, $myarray, "Hello World of PHP");

Now cleary the above will not work as it all it will do is replace the various vowels with the entire $myarray string, whereas I would like to replace all instances of a's with 1's, e's with 2's, i's with 3's etc.

Sure, it can be done individually, but that would be boring 🙂 I just think it would be handy if it all could all be done at once so that I could go and make some tea, whilst the script does all the work. 🙂

Every month I have to find and replace 38 instances of things like umlauts and other special characters and to be frank, it is boring and time consuming.

I dont doubt its possible, its just I cant think of a good approach of even whether str_replace and arrays are the best way forward.

Any help much appreciated.

    • [deleted]

    🙂 yay thank you!

      Originally posted by robwatts2
      Would I would like to try and do is something like

      // Provides: H2ll W4rld 4f PHP
      $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
      $myarray=array(("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
      $onlyconsonants = str_replace($vowels, $myarray, "Hello World of PHP");

      Now cleary the above will not work as it all it will do is replace the various vowels with the entire $myarray string, whereas I would like to replace all instances of a's with 1's, e's with 2's, i's with 3's etc.

      as laserlight suggest strtr is probably best but i would like to point out that your above example works as well

        Thankyou Sidney.

        I hadn't tested it. I just assumed that it wouldn't work.:rolleyes:

        Oh well, suffice to say that I am very happy, as my monthly chore is now simply a 2 minute stroll in the park.

        Thanks again, for your inputs.

          Write a Reply...