52

Has someone made a StringBuilder implementation in PHP?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mark
  • 67,098
  • 47
  • 117
  • 162
  • 3
    Why would you want something like this? – Ben Nov 18 '10 at 18:38
  • 5
    Related: [php String Concatenation, Performance](http://stackoverflow.com/q/124067/106224) (many of the answers do dismiss the need for such a class, but all with good reasons) – BoltClock Nov 18 '10 at 18:40
  • 1
    You don't need StringBuilder in PHP, see my answer: http://stackoverflow.com/questions/124067/php-string-concatenation-performance/16112845#16112845 – nightcoder Apr 19 '13 at 20:19

5 Answers5

98

Note:

This answer is from 2010, there might be stringbuilders that can improve the performance by now (judging from the comments below). I have not worked with php for a long time so my knowledge is not up to date. This answer might be out dated.

The following question might provide interesting information as well, all tough their conclusion seem to be the same.

php String Concatenation, Performance


Why do you want to use a StringBuilder? Strings in php are mutable. Therefore performance is not an issue.

Just build string like this

$string = "start";
$string .= "appended string";
$string .= "appended string";
etc.
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73
9

You can use sprintf which is only a basic version but requires no extra libraries, examples Follow

$String = "Firstname %s, lastname %s, Age %d";
echo sprintf($String,"Robert","Pitt",22);

And also handles type casting and position replacements:

$format = "The %2$s contains %1$d monkeys. That's a nice %2$s full of %1$d monkeys.";
sprintf($format, $num, $location);

All though i do like the look of jacob's answer :)

taker a look at the great functionality of t his function and its sister function here: http://php.net/manual/en/function.sprintf.php

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • 4
    @Webnet Well it's good for strings that are defined elsewhere (such as in a translation file). Otherwise it becomes nothing more than a magic string (which is the same concept as a [Magic Number](http://en.wikipedia.org/wiki/Magic_number) ) – ircmaxell Nov 18 '10 at 18:39
  • L18n is exactly the best use for this function and also its sister function for char conversions: `printf("char 65 = %c", 65);` – RobertPitt Nov 18 '10 at 18:44
4

There are some implementations out there, however I don't see why you would need a StringBuilder in PHP,at least not for performance reasons. Plain string concatenation in PHP is faster than sprintf or the impelementation Jacob suggested.

Jan Thomä
  • 13,296
  • 6
  • 55
  • 83
2

You don't need StringBuilder or StringBuffer in PHP , PHP is super handy I offer you , using from hereDoc and NowDoc if you you wish to keep PyString :

$YourString = "start";
$YourString .= <<<'EOD'

appended string
Example of string
spanning multiple lines
using nowdoc syntax.

EOD;

$YourString .= <<<buffer

appended string
Example of string
spanning multiple lines
using heredoc syntax.

appended string

appended string

buffer;
AMH
  • 461
  • 5
  • 12
  • based on your answer, I made a suggestion of using variables and class fields right inside of the HERE_DOC/NOW_DOC block. – Dimitry K Aug 15 '14 at 14:20
1

Answer of @Amir, gave me inspiration to the fact that in PHP if you want 'named parameters' or 'positional' parameters, you don't need sprintf, but HERE_DOC/NOW_DOC works perfect. You can even use this inside of a class for properties and call getters.

class MyClass{

     private $property;

     private $stock; // some other object with getter 'getSomeProperty()'

     function __toString(){

         $localvar = 'Localvar';
         $localvar2 = 'Localvar2';
         return <<<HERE_DOC
           {{ 
              fqsn: {$this->stock->getSomeProperty()},
              property: {$this->property},
              localvar: {$localvar},
              localvar2: $localvar2
           }}
HERE_DOC;
      } // end __toString()
 } // end MyClass
Dimitry K
  • 2,236
  • 1
  • 28
  • 37