Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

MySQL’s REPLACE() Is PHP’s STR_REPLACE()

20 Responses »

The PHP function that I use the most could be str_replace(). If you aren't familiar with the function, here's what it does:

$string = 'Hello_My_Friend';
$result = str_replace('_','-',$string);  //becomes Hello-My-Friend

Over the past months, I've grown to realize that I oftentimes use the function as a quick fix. I've been using MySQL's version of str_replace() a lot lately in an effort to cleanse the sins of my programming past. Here's a basic usage of MySQL's REPLACE function:

UPDATE website_content_pages SET content = REPLACE(content,'b>','strong>');

The above code changes all "<B>" and "</B>" tags to "<STRONG>" and "</STRONG>" tags. Using MySQL's function allows me to fix the problem once instead of "re-fixing" it every time the page loads.

Discussion

  1. tom
    March 7, 2008 @ 2:44 am

    Bit of an aside but just for everyone’s info, don’t forget that str_replace is pretty quick but strtr (string translate) is even faster.

    To use the same example:

    $string = ‘Hello_My_Friend’;
    $result = strtr($string, ‘_’, ‘-’); //becomes Hello-My-Friend

  2. jay
    March 7, 2008 @ 1:25 pm

    interesting..I haven’t thought of using that to clean up some old code.

  3. March 7, 2008 @ 1:32 pm

    Thats a big coincidence. I was going to replace an image link used on some content pages as well and I was going to use the same function to do it. Besides testing it on a test database or doing a db back up or something is there a safe way making sure that this doesnt mess up the table?

  4. March 7, 2008 @ 1:34 pm

    I always back up the table before doing this, Jesus. Too much can be lost!

  5. hex
    April 10, 2008 @ 5:39 am

    I have a problem when i use array to replace stuff, could you explain how its done the right way ?

  6. April 10, 2008 @ 4:06 pm

    Hex: Could you be more specific? Post a code example possibly?

  7. May 5, 2008 @ 11:09 am

    This perhaps is useful to some, then again what about if you replace text in a table when its not meant to be replaced? It could of been a letter directing to something else. So in your example this would get replaced also:

    dumb>some msg => dumstrong>some msg

    Makes no since to use REPLACE for them matters.

  8. June 17, 2008 @ 6:47 am

    Hi, I have a question. At our website people can post poems on our website. In dutch poems peolpe use a lot of > ‘ . But our website doesn’t seem to proces it. We get an error can we fix this by replacing it whit a rewrite?? Thanks Already!

  9. June 17, 2008 @ 7:13 am

    @sinterklaas: A replace sounds like a good idea for that, though I would make a backup before trying it.

  10. jacobain
    October 31, 2008 @ 8:10 am

    Wonderful! It works just fine even when used in the WHERE clause… thanks alot..

  11. December 12, 2008 @ 4:59 am

    Thank you for this. now i need to change my str_replace(); functions into mysql replace() function. it is more quicker than doing same job in php. blogged this

  12. December 12, 2008 @ 3:52 pm

    it is better then using php’s str_replace() function. You dont need to create loops only you use replace() it is more faster.

  13. March 29, 2009 @ 8:43 pm

    no need to backup..
    you simply

    create temporary table foo select * from bar;
    do stuff with foo;
    select * from foo;

    when its done foo disapears.. now if u like what u see you comment outter lines.. keeping the
    do stuff with foo.

  14. garrett griffin
    October 8, 2009 @ 9:38 am

    Thanks, exactly what I was looking for. I love blogs that help answer really simple questions with really simple answers.

  15. January 22, 2010 @ 11:06 pm

    thanks David, for REPLACE(content,’b>’,'strong>’);

  16. January 26, 2010 @ 1:54 pm

    @Tom: that’s a somewhat controversial statement. See the benchmark at http://net-beta.net/ubench/

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!