Add before and after the string

2019-04-18 01:58发布

问题:

$variable = 'for linebreak add 2 spaces at end';

Value of this variable everytime changes.

How to add some text or html before and after this string?


E.g. if we want to add '<div>' before and '</div>' after, string should look like:

$variable = '<div>for linebreak add 2 spaces at end</div>';

回答1:

Marko's solution is the way to go for simple cases. If you need to concatenate many strings, it is said that joining arrays is much faster.

$string[]='<div>';
$string[]= $variable;
$string[]='</div>';
$string = join('',$string);


回答2:

$wrapped_variable = '<div>' . $variable . '</div>';


回答3:

I'm not sure I understand what you want. Is this it?

<?php
function add_div($string){return '<div>'.$string.'</div>';}
$variable = 'for linebreak add 2 spaces at end'; 
echo add_div($variable); // <div>for linebreak add 2 spaces at end</div>
?>

If it isn't, say so and I will try again the next time I visit this page.



回答4:

What is the faster ways?

1. Join arrays?

1.1 Join arrays (with unset)

The code below :

for($i=1;$i<1000;$i++){
    $string[]='lorem_';
    $string[]= $i;
    $string[]='_ipsum';
    $string = join('',$string);
    var_dump($string);
    unset($string);
}

generate this average performance:

.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
|   5.6   |      0.016     |     0.061    |    20.60     |
|   7.0   |      0.009     |     0.010    |    16.69     |
|   7.1   |      0.011     |     0.011    |    24.18     |
|   7.2   |      0.003     |     0.017    |    19.41     |
'---------'----------------'--------------'--------------'

source: https://3v4l.org/8YK1H/perf#output


1.2 Join arrays (without unset)

The code below :

for($i=1;$i<1000;$i++){
    $string = [];
    $string[]='lorem_';
    $string[]= $i;
    $string[]='_ipsum';
    $string = join('',$string);
    var_dump($string);
    // unset($string);
}

generate this average performance:

.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
|   5.6   |      0.017     |     0.068    |    20.58     |
|   7.0   |      0.012     |     0.013    |    16.13     |
|   7.1   |      0.021     |     0.012    |    23.23     |
|   7.2   |      0.013     |     0.010    |    17.65     |
'---------'----------------'--------------'--------------'

source: https://3v4l.org/kU4QG/perf#output


2. Shorthand operator?

2.1 Shorthand operator (with unset)

The code below :

for($i=1;$i<1000;$i++){
    $string = 'lorem_';
    $string .= $i;
    $string .= '_ipsum';
    // $string = join('',$string);
    var_dump($string);
    unset($string);
}

generate this average performance:

.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
|   5.6   |      0.012     |     0.059    |    20.60     |
|   7.0   |      0.008     |     0.011    |    16.65     |
|   7.1   |      0.014     |     0.012    |    24.22     |
|   7.2   |      0.012     |     0.009    |    19.08     |
'---------'----------------'--------------'--------------'

source: https://3v4l.org/cCoSC/perf#output


2.2 Shorthand operator (without unset)

The code below :

for($i=1;$i<1000;$i++){
    $string = 'lorem_';
    $string .= $i;
    $string .= '_ipsum';
    // $string = join('',$string);
    var_dump($string);
    // unset($string);
}

generate this average performance:

.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
|   5.6   |      0.011     |     0.059    |    20.60     |
|   7.0   |      0.008     |     0.011    |    16.70     |
|   7.1   |      0.014     |     0.011    |    24.22     |
|   7.2   |      0.010     |     0.010    |    19.33     |
'---------'----------------'--------------'--------------'

source: https://3v4l.org/iKnaV/perf#output


3. One line concat?

3.1 One line concatenation (with unset)

The code below :

for($i=1;$i<1000;$i++){
    $string = 'lorem_' . $i . '_ipsum';
    // $string .= $i;
    // $string .= '_ipsum';
    // $string = join('',$string);
    var_dump($string);
    unset($string);
}

generate this average performance:

.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
|   5.6   |      0.015     |     0.066    |    20.60     |
|   7.0   |      0.008     |     0.012    |    16.68     |
|   7.1   |      0.014     |     0.014    |    24.23     |
|   7.2   |      0.010     |     0.014    |    19.45     |
'---------'----------------'--------------'--------------'

source: https://3v4l.org/tv4X0/perf#output


3.2 One line concatenation (without unset)

The code below :

for($i=1;$i<1000;$i++){
    $string = 'lorem_' . $i . '_ipsum';
    // $string .= $i;
    // $string .= '_ipsum';
    // $string = join('',$string);
    var_dump($string);
    // unset($string);
}

generate this average performance:

.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
|   5.6   |      0.012     |     0.056    |    20.65     |
|   7.0   |      0.008     |     0.008    |    16.69     |
|   7.1   |      0.014     |     0.010    |    24.17     |
|   7.2   |      0.007     |     0.011    |    19.39     |
'---------'----------------'--------------'--------------'

source: https://3v4l.org/mF0YW/perf#output


If merge all results by PHP version:

PHP 5.6

.-------------.----------------.--------------.--------------.
|     Code    | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
|         1.1 |      0.016     |     0.061    |    20.60     |
|         1.2 |      0.017     |     0.068    | >> 20.58     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 |      0.0165    |     0.0645   | >> 20.59     |
|-------------|----------------|--------------|--------------|
|         2.1 |      0.012     |     0.059    |    20.60     |
|         2.2 |   >> 0.011     |     0.059    |    20.60     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 |   >> 0.0115    |  >> 0.059    |    20.60     |
|-------------|----------------|--------------|--------------|
|         3.1 |      0.015     |     0.066    |    20.60     |
|         3.2 |      0.012     |  >> 0.056    |    20.65     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 |      0.0135    |     0.061    |    20.625    |
'-------------'----------------'--------------'--------------'

PHP 7.0

.-------------.----------------.--------------.--------------.
|     Code    | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
|         1.1 |      0.009     |     0.010    |    16.69     |
|         1.2 |      0.012     |     0.013    | >> 16.13     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 |      0.0105    |     0.0115   | >> 16.41     |
|-------------|----------------|--------------|--------------|
|         2.1 |   >> 0.008     |     0.011    |    16.65     |
|         2.2 |   >> 0.008     |     0.011    |    16.70     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 |   >> 0.008     |     0.011    |    16.675    |     
|-------------|----------------|--------------|--------------|
|         3.1 |   >> 0.008     |     0.012    |    16.68     |
|         3.2 |   >> 0.008     |  >> 0.008    |    16.69     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 |   >> 0.008     |  >> 0.010    |    16.685    |
'-------------'----------------'--------------'--------------'

PHP 7.1

.-------------.----------------.--------------.--------------.
|     Code    | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
|         1.1 |   >> 0.011     |     0.011    |    24.18     |
|         1.2 |      0.021     |     0.012    | >> 23.23     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 |      0.016     |  >> 0.0115   | >> 23.705    |
|-------------|----------------|--------------|--------------|
|         2.1 |      0.014     |     0.012    |    24.22     |
|         2.2 |      0.014     |     0.011    |    24.22     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 |   >> 0.014     |  >> 0.0115   |    24.22     |
|-------------|----------------|--------------|--------------|
|         3.1 |      0.014     |     0.014    |    24.23     |
|         3.2 |      0.014     |  >> 0.010    |    24.17     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 |   >> 0.014     |     0.012    |    24.20     |
'-------------'----------------'--------------'--------------'

PHP 7.2

.-------------.----------------.--------------.--------------.
|     Code    | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
|         1.1 |   >> 0.003     |     0.017    |    19.41     |
|         1.2 |      0.013     |     0.010    | >> 17.65     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 |   >> 0.008     |     0.0135   | >> 18.53     |
|-------------|----------------|--------------|--------------|
|         2.1 |      0.012     |  >> 0.009    |    19.08     |
|         2.2 |      0.010     |     0.010    |    19.33     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 |      0.011     |  >> 0.0905   |    19.205    |
|-------------|----------------|--------------|--------------|
|         3.1 |      0.010     |     0.014    |    19.45     |
|         3.2 |      0.007     |     0.011    |    19.39     |
|             |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 |      0.0085    |     0.0125   |    19.42     |
'-------------'----------------'--------------'--------------'

Conclusion

  • For the system time, the faster way (0.003 second) is to use join arrays (with unset) in PHP 7.2.0.

  • For the user time, the faster way (0.003 second) is to use oneline concatenation without unset in PHP 7.0.22.

  • For memory usage, the "most ecomonical" (14.47 MiB) way is to use join array without unset in PHP 7.0.14.