views:

94

answers:

3
$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>';
+8  A: 
$wrapped_variable = '<div>' . $variable . '</div>'
Marko
its not a solution
Happy
it IS a solution
Marko
@Happy: why you think this is not a solution?
DaNieL
+1  A: 

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);
pixeline
A: 

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.

matsolof