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 Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Replace the word "and"

    Im trying to get the preg_replace to replace the word and but cant seem to get it to work

    preg_replace('/ And /i',' og ',$firstname) ;
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hi, try this:
    PHP Code:
    preg_replace('/\sand\s/i',' og ',$firstname) ; 

  • #3
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you very mutch

    Can you also tell me if i can combine multi replace in one command ??


    $firstname = preg_replace('/\sog\s/i',' og ',$firstname);
    $firstname = preg_replace('/\sand\s/i',' and ',$firstname);
    $firstname = preg_replace('/\svon\s/i',' von ',$firstname);
    $firstname = preg_replace('/\svan\s/i',' van ',$firstname);

    Yes it may look wierd but i have a
    $firstname = mb_convert_case(trim($_POST["firstname"]), MB_CASE_TITLE, "utf-8");
    to make shure the names are with capital letters .. but those word i dont want to be capital
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    PHP Code:
    $firstname preg_replace('/\s(og|and|von|van)\s/ie','" ".strtolower("\\1")." "',$firstname); 
    didn't tested but should works
    oops,code edited, because you want to output spaces before and after the string...
    Last edited by koko5; 02-16-2010 at 09:56 PM. Reason: output spaces before and after the string

  • #5
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks
    Aha ..( not [

    That strtolower is smart

    Ill try it out
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"


  •  

    Posting Permissions

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