tags:

views:

57

answers:

3

oops sorry.. i need to add it to multi line, here the form file

<?php

if (!isset($_POST['submit'])) {
    echo "<form method=\"post\" action=\"" .$_SERVER['PHP_SELF']. "\">";
    echo "Info Box:<textarea name=\"infoBox\" /></textarea><br />";
    echo "<input type=\"submit\" value=\"submit\" name=\"submit\" />";
    echo "</form>";
}
else {
$infoBox = $_POST["infoBox"];
    echo "<textarea>";
    echo $infoBox;
    echo "</textarea>";
}
?>

example input file:

test
hello
world

and the output should be

<b>test</b>
<b>hello</b>
<b>world</b>

============================================================

hello, i need to add prefix and suffix in php like

hello

to

<b>hello</b>

thanks for the response.

A: 
$var = '<b>' . $var . '</b>';
Alnitak
A: 

$string = "prefix".$string."suffix"; ?

Alin Purcaru
+1  A: 

There are various ways. Depending on what you actually want to do with the result, one might be better than the other:

  • String concatenation: More flexible, as you can define prefix and suffix independently.

    $content = 'hello';
    $html = '<b>' . $content . '</b>';
    
  • sprintf(): Better if you want to embed a value into a certain format.

    $html = sprintf('<b>%s</b>', $content);
    
  • Embed PHP in HTML: If you want to output HTML, consider to embed PHP into HTML, not vice versa.

    <b><?php echo $content; ?></b>
    

Update:

I'm not sure where

test
hello
world

comes from, but assuming that at the end of each line is just the newline-character \n, you can do it in a functional way:

 function wrap($w) {
    return '<b>' . $w . '</b>';
}

$s = "test\nhello\nworld";
$s = implode("\n", array_map('wrap', explode("\n", $s)));

See: implode(), array_map(), explode()

or with an explicit loop:

$s = "test\nhello\nworld";
$words = explode("\n", $s);

foreach($words as &$word) {
    $word = '<b>' . $word . '</b>';
}

$s = implode("\n", $words);
Felix Kling
i'm really sorry for my bad question, i have update it above.
ewwink
your code return <b>testhelloworld</b> not <b>test</b><b>hello</b><b>world</b>
ewwink
@ewwink: I did not update my answer yet. Please see the update now.
Felix Kling