SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String manipulation

    I was wondering what's the mos efficient way to replace values in a string. Currently I'm using str_replace() to do simple replaces that doesn't need regex, but I've noticed that there are similar functions for this.

    Are there any advantages in using other functions for this, and in which cases?

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just do a benchmark!

    Build a script that loops 500 times for a string replace with str_replace and then with regex and see how it does overall.

    My test $string

    $start_sr = time( );

    for( ) {
    str_replace()
    }

    $end_sr = time();

    $start_reg = time();

    for( ) {
    preg_replace()
    }

    $end_reg = time();

    Compare

    Voilą!
    David

  3. #3
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's not a fair comparison since preg_replace can do lots the str_replace can't.

  4. #4
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm.. so since I'm not using regular expressions to replace valeus in this case, I should just go with str_replace?

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using regular expressions always involves an additional overhead. So don't use them if you don't need them.

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lix0r View Post
    hmm.. so since I'm not using regular expressions to replace valeus in this case, I should just go with str_replace?
    Quote Originally Posted by Kailash Badu View Post
    using regular expressions always involves an additional overhead. So don't use them if you don't need them.
    This is utter nonsense; regex will always do a better job even when just working on string literals.

  7. #7
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    This is utter nonsense; regex will always do a better job even when just working on string literals.
    wow ! that's a new one.

    So you would want to use preg_replace() , litter it with you regex, and force your script to invoke the regex engine even when str_replace() can do the job very fine.

    I can guess why PHP team finds it very hard to teach people into using light coding practices. With people like you, they sure are going to have a hard time. Nonetheless, you can call your cow a horse. best of luck.

  8. #8
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I'll just do a benchmark test, to determin what's the most efficient way manipulating the strings. If there are no great benefits of using preg_replace, I'll just keep using str_replace. I don't see the need to fool around with regex for things this simple.. "Keep it simple, stupid!"

  9. #9
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace() is certainly more powerful than str_replace. You can do a lot of things with it that you cannot with str_replace() . But that doesn't mean you ought to use it even for the simplest of replace operations. It's like driving a bull dozer to commute to your workplace instead of a car.


Bookmarks

Posting Permissions

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