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 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    using preg_replace to change a string

    Hi All

    I am wondering if it is possible to change a string e.g
    scrubs series 1 => scrubs series one
    scrubs series one => scrubs series 1

    basically if the string contains either a number below 10 or its equivelant word i want it changed to the other

    e.g
    1 => one
    one => 1
    2 => two
    two => 2
    etc...

    is this possible?

    also can i use preg replace to check for a couple of words e.g season,series and replace it with the other, so if the title was scrubs season 1 it would change it to scrubs series 1 and if it found series it would replace it with season?

    many thanks

    Luke
    Last edited by LJackson; 11-19-2010 at 06:48 PM.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    I don't know what do you have in the beginning, so i can suggest to use str_replace
    PHP Code:
    $search = array(1,2,3,4,5);
    $replace = array('one''two''three''four''five');
    echo 
    str_replace($search$replace'scrubs series 1'); 
    or

    PHP Code:
    $search = array('1' => 'one'
            
    'one' => '1');
    echo 
    strtr('scrubs series one'$search);
    echo 
    strtr('scrubs series 1'$search); 
    Last edited by poyzn; 11-19-2010 at 07:12 PM.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, you could use preg_replace, but its inefficient for this I'd say. Go for str_replace like poyzn suggested.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok thanks!

    one little problem im having is is that if the string contains another number other in the array 32 for example it will change it to threetwo which i'd expect it to but as 32 is not in the array could this be prevented?

    my code is like so
    PHP Code:
            $find = array(1,2,3,4,5,6,7,8,9,10);
            
    $replace = array('one''two''tree''four''five','six','seven','eight','nine','ten');
            
    $search[3] = str_replace($find$replace$keywords);  
            
    $search[4] = str_replace($replace$find$keywords); 
    so technically 32 is not in there to be replaced by anything?
    any ideas please

    cheers

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You'd need to either space your numbers:

    Code:
    $find = array(' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 10 ');
    or else use preg_replace for that level of control.

  • Users who have thanked MattF for this post:

    LJackson (11-19-2010)

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Or do the str_replace on your $search array in a "for" loop and check the strlen() for each entry and only make the replacement for a particular entry when that length is less than 2 (in other words, when the number is greater than 9)...

    It's not terribly fancy, but there is usually more than one way to skin a cat. If you aren't familiar with regex stuff then this would make for a much easier fix if you need to get going with this right away while you learn regex syntax.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    LJackson (11-19-2010)

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Word boundaries would be an effective solution.

    A word boundary is marked as /b
    PHP Code:
    $string 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

    $replacements = array(
        
    1  => 'one',
        
    2  => 'two',
        
    3  => 'three',
        
    4  => 'four',
        
    5  => 'five',
        
    6  => 'six',
        
    7  => 'seven',
        
    8  => 'eight',
        
    9  => 'nine',
        
    10 => 'ten',
    );

    foreach ( 
    $replacements as $int => $word )
    {
        
    $string preg_replace'/\b' $int '\b/'$word$string );
    }

    echo 
    $string
    ...or run preg_replace() once by preparing the expressions ahead of time:
    PHP Code:
    $string 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

    $replacements = array(
        
    '/\b1\b/'  => 'one',
        
    '/\b2\b/'  => 'two',
        
    '/\b3\b/'  => 'three',
        
    '/\b4\b/'  => 'four',
        
    '/\b5\b/'  => 'five',
        
    '/\b6\b/'  => 'six',
        
    '/\b7\b/'  => 'seven',
        
    '/\b8\b/'  => 'eight',
        
    '/\b9\b/'  => 'nine',
        
    '/\b10\b/' => 'ten',
    );

    $string preg_replacearray_keys$replacements ), $replacements$string );

    echo 
    $string
    Output:
    Code:
    This is one, not 31. This is two or three, but not 32 or 33, got it?

  • Users who have thanked kbluhm for this post:

    LJackson (11-19-2010)

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    wow didnt relise there were so many ways of acheiving it

    thanks very much all!!!!!!!
    Luke

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by kbluhm View Post
    Word boundaries would be an effective solution.

    A word boundary is marked as /b
    PHP Code:
    $string 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

    $replacements = array(
        
    1  => 'one',
        
    2  => 'two',
        
    3  => 'three',
        
    4  => 'four',
        
    5  => 'five',
        
    6  => 'six',
        
    7  => 'seven',
        
    8  => 'eight',
        
    9  => 'nine',
        
    10 => 'ten',
    );

    foreach ( 
    $replacements as $int => $word )
    {
        
    $string preg_replace'/\b' $int '\b/'$word$string );
    }

    echo 
    $string
    ...or run preg_replace() once by preparing the expressions ahead of time:
    PHP Code:
    $string 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

    $replacements = array(
        
    '/\b1\b/'  => 'one',
        
    '/\b2\b/'  => 'two',
        
    '/\b3\b/'  => 'three',
        
    '/\b4\b/'  => 'four',
        
    '/\b5\b/'  => 'five',
        
    '/\b6\b/'  => 'six',
        
    '/\b7\b/'  => 'seven',
        
    '/\b8\b/'  => 'eight',
        
    '/\b9\b/'  => 'nine',
        
    '/\b10\b/' => 'ten',
    );

    $string preg_replacearray_keys$replacements ), $replacements$string );

    echo 
    $string
    Output:
    Code:
    This is one, not 31. This is two or three, but not 32 or 33, got it?
    Hi Kbluhm

    the above works great
    however i've tried changing it so that if it finds the word eg one then it will replace it with the key like so
    PHP Code:
    $string preg_replace($replacements,  array_keys$replacements ), $keywords ); 
    but it returns a blank match? any ideas where im going wrong
    thanks mate
    Luke


  •  

    Posting Permissions

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