...

View Full Version : Adding <b> tags to $str, where $str is seperated by linebreaks



Dale_G
03-12-2008, 03:11 AM
Hello everybody, I'll try to make this as simple as I can.

I have a home made very basic News CMS system. Has a form where I can input the data, and writes it to an SQL Database.

Some fields I have are title, author, date, and body. Now, the main body field is a text area in which whatever I enter, that will be the main body content for the news article.

This is it...



<tr>
<td><font size='2' face='Arial, Helvetica, sans-serif'>Body:</font></td>
<td><textarea name='body' cols='60' rows='9'></textarea></td>
</tr>


..And it gets accessed by this when I submit it!


$body = ($HTTP_POST_VARS['body']);

Pretty basic...

But now what I'd like to do is seperate the body content by LINE BREAK and enclose each seperated text with <b></b> tags.

So basically, say this is what I enter.



Oh, hey everyone! What's up?

This is a news article.

I like it very much! Lol. =)


I'D LIKE IT TO BECOME THIS...




<b>Oh, hey everyone! What's up?</b>

<b>This is a news article.</b>

<b>I like it very much! Lol. =)</b>


You see how that works out? The text would be separated by the line breaks, or the spaces in between them, and then at the beginning of it would be a <b> tag and at the end a closing </b> tag.

So, I've been working on ways to do this, but I don't really have a firm grasp on it...Perhaps adding a function that does this?


function convtobr($str)
{
$str = explode("/n" $str);
$str = ("<b>$str</b>");
return $str;
}

This is just a notion of a function, not working and..I don't think it's proper, however it may point someone in the right direction.

Well, thanks everyone! :)

oesxyl
03-12-2008, 03:21 AM
hi,



function convtobr($str) {
$res = '';
$str = explode("\n" $str);
foreach($str as $s){
$res =. '<b>'.$s.'</b>';
}
return $res;
}



newline is "\n" not "/n"



best regards

Dale_G
03-12-2008, 04:37 AM
hi,



function convtobr($str) {
$res = '';
$str = explode("\n" $str);
foreach($str as $s){
$res =. '<b>'.$s.'</b>';
}
return $res;
}



newline is "\n" not "/n"



best regards

I'm afraid it's returning an error.

Parse error: parse error, unexpected '.' in XXXXXX on line 221

Line 221 is


$res =. '<b>'.$s.'</b>';

_Aerospace_Eng_
03-12-2008, 04:40 AM
Missing a comma in the explode function (reading the manual helps in simple mistakes like this), and to concatenate the $res variable the . (dot) goes in front of the = (equals) sign.

function convtobr($str) {
$res = '';
$str = explode("\n", $str);
foreach($str as $s){
$res .= '<b>'.$s.'</b>';
}
return $res;
}
Also you are using this

$body = ($HTTP_POST_VARS['body']);
which is deprecated. You should be using

$body = $_POST['body'];
Also tables aren't for page layout, on forms they can be okay but the font element is also deprecated. Use CSS instead.

kbluhm
03-12-2008, 04:41 AM
Change that line to:

$res .= '<b>'.$s.'</b>';

Come on Dale, that is a simple bug/typo. This is PHP 101 stuff. Would your PHP education continue if we were all to die in a plane crash tomorrow? ;)

oesxyl
03-12-2008, 04:41 AM
I'm afraid it's returning an error.

Parse error: parse error, unexpected '.' in XXXXXX on line 221

Line 221 is


$res =. '<b>'.$s.'</b>';

it's a comma there ',' and must be a dot '.'. Is a very simple problem, I don't test the code and I don't assume that you will be cut and past it, :)


there is no comma, must .= instead of =. , perl habits, :)


best regards

Dale_G
03-12-2008, 01:40 PM
Missing a comma in the explode function (reading the manual helps in simple mistakes like this), and to concatenate the $res variable the . (dot) goes in front of the = (equals) sign.

function convtobr($str) {
$res = '';
$str = explode("\n", $str);
foreach($str as $s){
$res .= '<b>'.$s.'</b>';
}
return $res;
}
Also you are using this

$body = ($HTTP_POST_VARS['body']);
which is deprecated. You should be using

$body = $_POST['body'];
Also tables aren't for page layout, on forms they can be okay but the font element is also deprecated. Use CSS instead.

I correctly that myself, and it still didn't seem to work.

Dale_G
03-12-2008, 01:41 PM
Change that line to:

$res .= '<b>'.$s.'</b>';

Come on Dale, that is a simple bug/typo. This is PHP 101 stuff. Would your PHP education continue if we were all to die in a plane crash tomorrow? ;)

I don't see any difference between your line of code and my current line of code?

oesxyl
03-12-2008, 06:56 PM
I don't see any difference between your line of code and my current line of code?



function convtobr($str) {
$res = '';
$str = explode("\n", $str);
foreach($str as $s){
$res .= '<b>'.$s.'</b>';
}
return $res;
}


- _Aerospace_Eng_ said in his post about missing comma, I don't see it before.
- kbluhm correct the dot I put it wrong in my replay

It must work now

PS: if you intend to copy and past, do it from _Aerospace_Eng_ replay to avoid problem with icode tags from this replay.

best regards

Andrew Johnson
03-12-2008, 07:14 PM
Use \r\n instead of \n

oesxyl
03-12-2008, 07:28 PM
Use \r\n instead of \n

yes, we need more mistakes to this thread, at list now we are two :)
I pressume you don't say to replace all \n with \r\n and then replace \r\r
with \r, to have only \r\n, :)
AFAIK on mac they use only \r, so you probably must look for a solution
for this too.

best regards

Dale_G
03-13-2008, 01:09 PM
function convtobr($str) {
$res = '';
$str = explode("\n", $str);
foreach($str as $s){
$res .= '<b>'.$s.'</b>';
}
return $res;
}


- _Aerospace_Eng_ said in his post about missing comma, I don't see it before.
- kbluhm correct the dot I put it wrong in my replay

It must work now

PS: if you intend to copy and past, do it from _Aerospace_Eng_ replay to avoid problem with icode tags from this replay.

best regards

Okay, well Aerospace_Eng's code seems to do somewhat what I'm looking for.

Here is all of the code for the section it involves.


/*
+-------------------------------------------------------
| if submitnews, submitting the news to the sql db
+-------------------------------------------------------
*/

function convtobr($str) {
$res = '';
$str = explode("\n", $str);
foreach($str as $s){
$res .= '<b>'.$s.'</b>';
}
return $res;
}

if ($action == "submitnews" && $username != "")
{

$icon = ($_POST['icon']);
$category = ($_POST['category']);
$date = ($_POST['date']);
$ldate = ($_POST['ldate']);
$title = ($_POST['title']);
$smallbody = ($_POST['smallbody']);
$body = convtobr($_POST['body']);
$mysql_query = "INSERT INTO `news` ( `icon`, `category`, `date`, `ldate`, `title`, `smallbody`, `body` ) VALUES ('".$icon."', '".$category."', '".$date."', '".$ldate."', '".$title."', '".$smallbody."', '".$body."' )";
mysql_query($mysql_query);
echo "<font face='Arial' size=2>News Added!<br><br><a href='?action=verified'>Go Home</a></font>";
}


And, that code turns


One.

Two.

Into:


<b>One.
</b><b>
</b><b>Two.</b>

I'm looking for it to simply be:


<b>One.</b>

<b>Two.</b>

And for this particular application, it's not possible just for the entire contents of the text area to be bold, trust me if it was that simple i'd just stick some <b></b> tags in front of it.

oesxyl
03-13-2008, 02:43 PM
function convtobr($str) {
$res = '';
$str = explode("\n", $str);
foreach($str as $s){
if(strlen($s) > 0)
$res .= '<b>'.$s.'</b>';
}
return $res;
}


best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum