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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Regular Expression

    Hi

    String: Conan DJ ~ I'm Not A Kid
    String: History's Strongest Disciple Kenichi

    Basically i want to make a regular expression to extract only "ALPHABET" string from the above strings and replace space with _, no numbers, no symbols. Anybody can give a solution?


    Regards
    XD
    Last edited by xiaodao; 03-20-2009 at 11:31 AM.
    flying dagger

  • #2
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    String: Conan DJ ~ I'm Not A Kid
    String: History's Strongest Disciple Kenichi
    PHP Code:
    $str1 "Conan DJ ~ I'm Not A Kid";
    $str2 "History's Strongest no. 1 Disciple Kenichi the 2nd";

    /* Each separate space replaced with _: */
      
    $str1 preg_replace("/[^a-zA-Z ]/"""$str1);  /* Keeps only a-z, A-Z and space */
      
    $str1 preg_replace("/[ ]/""_"$str1);
      echo 
    $str1."\r\n";

    /* => Conan_DJ__Im_Not_A_Kid */

    /* 1 or more consecutive spaces replaced with _: */
      
    $str1 preg_replace("/[^a-zA-Z ]/"""$str1);  /* Keeps only a-z, A-Z and space */
      
    $str1 preg_replace("/[ ]+/""_"$str1);
      echo 
    $str1."\r\n";

    /* => Conan_DJ_Im_Not_A_Kid */

    /* All-in-one replacement expression */
      
    echo $str2."  =>  ";
      
    $str2 preg_replace("/[ ]+/""_"preg_replace("/[^a-zA-Z ]/"""$str2));
      echo 
    $str2."\r\n";

    /* => Historys_no_Strongest_Disciple_Kenichi_the_nd */ 
    There are probably more artful ways to do this.

    wb4php


  •  

    Posting Permissions

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