Jump to content

[SOLVED] Adding <b> tags to $str, where $str is seperated by linebreaks


Dale_G

Recommended Posts

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! :)

Link to comment
Share on other sites

<?php
$string  = "hello <br>";
$string .= "dog <br>";
$string .= "Phpsensei <br>";

$string  = explode("<br>",$string); // break into seperate lines
foreach($string a $line){

echo '<b>' . $line . '</b>';

}
?>

 

This does not seem to work for me.

 

They do not get bolded. Keep in mind I am using this code to insert into the SQL DB.

 

$mysql_query = "INSERT INTO `news` ( `icon`, `category`, `date`, `ldate`, `title`, `smallbody`, `body` ) VALUES ('".$icon."', '".$category."', '".$date."', '".$ldate."', '".$title."', '".$smallbody."', '".$line."' )";

Link to comment
Share on other sites

I forgot the "s"

 

try

 

<?php
$string  = "hello <br>";
$string .= "dog <br>";
$string .= "Phpsensei <br>";

$string  = explode("<br>",$string); // break into seperate lines
foreach($string as $line){

$string = '<b>' . $line . '</b>';

// Insert into the database

}
?>

Link to comment
Share on other sites

I forgot the "s"

 

try

 

<?php
$string  = "hello <br>";
$string .= "dog <br>";
$string .= "Phpsensei <br>";

$string  = explode("<br>",$string); // break into seperate lines
foreach($string as $line){

$string = '<b>' . $line . '</b>';

// Insert into the database

}
?>

 

Yes, I corrected that.

 

But to no avail. :(

Link to comment
Share on other sites

A much simpler way would be to use the implode() function:

<?php
$in_str = "This string\nhas line breaks\nin it";
$bold_str = '<b>' . implode('</b><br><b>',explode("\n",$in_str)) . '</b>';
echo $bold_str;
?>

 

Ken

 

Hmm...this one seems to do something. But, not what I'm looking for.

 

It turns

hey.

how are you?

 

into

 

<b>hey.
</b><br><b>
</b><br><b>how are you?</b>

 

Where it SHOULD be....

 

<b>hey.</b>

<b>how are you?</b>

Link to comment
Share on other sites

<?php
  function add_format($str)
  {
  return (!empty($str)?"<B>$str</B>":'');
  }
  $str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
  
  $lines=implode("\n",array_map('add_format',explode("\n",$str)));
  header('Content-type: text/plain');
  echo $lines;
?>

Link to comment
Share on other sites

You could also forget about formating each string separately and use inline CSS with a <span> and the nl2br() function:

<?php
$str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
echo '<span style="font-weight:bold">' . nl2br($str) . '</span>';
?>

 

Ken

Link to comment
Share on other sites

<?php
  function add_format($str)
  {
  return (!empty($str)?"<B>$str</B>":'');
  }
  $str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
  
  $lines=implode("\n",array_map('add_format',explode("\n",$str)));
  header('Content-type: text/plain');
  echo $lines;
?>

 

Hmm, this almost does what I'm trying to do, except it turns.

 

One.

Two.

 

Into...

 

<B>One.
</B>
<B>
</B>
<B>Two.</B>

 

Where it should be.

<b>One.</b>

<b>Two.</b>

Link to comment
Share on other sites

You could also forget about formating each string separately and use inline CSS with a <span> and the nl2br() function:

<?php
$str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
echo '<span style="font-weight:bold">' . nl2br($str) . '</span>';
?>

 

Ken

 

For this type of application that would not work, if it was that simple, i'd just have that span code in place to begin with, none of this tinkering with HTTP_POST_VARS and formatting that!

Link to comment
Share on other sites

Then please explain why you need to have the strings exactly as you're requesting. We have given you a number of solutions that seem to meet your requirement.

 

I will give you one more:

<?php
$str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
foreach(explode("\n",$str) as $line)
     if (strlen(trim($line)) > 0) echo '<b>' . $line . '</b><br>';
     else echo '<br>';
?>

 

Ken

Link to comment
Share on other sites

Then please explain why you need to have the strings exactly as you're requesting. We have given you a number of solutions that seem to meet your requirement.

 

I will give you one more:

<?php
$str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
foreach(explode("\n",$str) as $line)
     if (strlen(trim($line)) > 0) echo '<b>' . $line . '</b><br>';
     else echo '<br>';
?>

 

Ken

 

Okay well, I'll just post my code from the section this involves.

 

	/*
+-------------------------------------------------------
| if submitnews, submitting the news to the sql db
+-------------------------------------------------------
*/
  
if ($action == "submitnews" && $username != "")
	{

		$icon = ($_POST['icon']);
		$category = ($_POST['category']);
		$date = ($_POST['date']);
		$ldate = ($_POST['ldate']);
		$title = ($_POST['title']);
		$smallbody = ($_POST['smallbody']);
		$body = ($_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>";
	}

 

Also, specifically for this I'd be using <p></p> tags!

 

So, you can see why they'd need to be repeated for each line.

Link to comment
Share on other sites

Also, specifically for this I'd be using <p></p> tags!

So, why didn't you say that in the first place? I don't see where you want to use this solution.

 

I hope you realize, that you're opening yourself up to possible SQL injection with your code. At the very least do something like:

 

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

		$icon = mysql_real_escape_string(stripslashes($_POST['icon']));
		$category = mysql_real_escape_string(stripslashes($_POST['category']));
		$date = mysql_real_escape_string(stripslashes($_POST['date']));
		$ldate = mysql_real_escape_string(stripslashes($_POST['ldate']));
		$title = mysql_real_escape_string(stripslashes($_POST['title']));
		$smallbody = mysql_real_escape_string(stripslashes($_POST['smallbody']));
		$body = mysql_real_escape_string(stripslashes($_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>";
	}
?>

 

Ken

Link to comment
Share on other sites

Also, specifically for this I'd be using <p></p> tags!

So, why didn't you say that in the first place? I don't see where you want to use this solution.

 

I hope you realize, that you're opening yourself up to possible SQL injection with your code. At the very least do something like:

 

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

		$icon = mysql_real_escape_string(stripslashes($_POST['icon']));
		$category = mysql_real_escape_string(stripslashes($_POST['category']));
		$date = mysql_real_escape_string(stripslashes($_POST['date']));
		$ldate = mysql_real_escape_string(stripslashes($_POST['ldate']));
		$title = mysql_real_escape_string(stripslashes($_POST['title']));
		$smallbody = mysql_real_escape_string(stripslashes($_POST['smallbody']));
		$body = mysql_real_escape_string(stripslashes($_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>";
	}
?>

 

Ken

 

Not sure, I'm saying it now though. I'd like to use it, because that's how I'd normally write my news articles. Each line would have be enclosed in <p></p> tags so they would be spaced and even.

 

Also, this form is on a page that is non public and protected by a log in which only I can access.

Link to comment
Share on other sites

<?php
  function add_format($str)
  {
  return (!empty($str)?"<B>$str</B>":'');
  }
  $str="Oh, hey everyone! What's up?\nThis is a news article.\n\nI like it very much! Lol. =)";
  
  $lines=implode("\n",array_map('add_format',explode("\n",$str)));
  header('Content-type: text/plain');
  echo $lines;
?>

 

Hmm, this almost does what I'm trying to do, except it turns.

 

One.

Two.

 

Into...

 

<B>One.
</B>
<B>
</B>
<B>Two.</B>

 

Where it should be.

<b>One.</b>

<b>Two.</b>

 

 

Not shure which php version u are using, but I get the output u expected, not the one u say u got.

add_format function returns <B>$str</B> or an empty line if $str is empty, so there is no newline in between the <B> tags

 

the first thing it does, is explode on newlines, so there is no newlines in the array.

then it adds the <B> Tags to each array line if necessary (no newlines added).

then after this is done, it concats the array adding the newlines, thus the tags are on the same line

 

so either, u didnt test the code, or u got a messed up php installation.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.