Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    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...

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

    Code:
    $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.

    Code:
    Oh, hey everyone! What's up?
    
    This is a news article.
    
    I like it very much! Lol. =)
    I'D LIKE IT TO BECOME THIS...


    Code:
    <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?

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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    hi,

    PHP Code:
    function convtobr($str)  {
            
    $res '';
            
    $str explode("\n" $str);
            foreach(
    $str as $s){
                
    $res =. '<b>'.$s.'</b>';
            }
            return 
    $res;
      } 
    Edit:
    newline is "\n" not "/n"



    best regards
    Last edited by oesxyl; 03-12-2008 at 02:23 AM.

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    hi,

    PHP Code:
    function convtobr($str)  {
            
    $res '';
            
    $str explode("\n" $str);
            foreach(
    $str as $s){
                
    $res =. '<b>'.$s.'</b>';
            }
            return 
    $res;
      } 
    Edit:
    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

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

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    PHP Code:
    function convtobr($str)  {
            
    $res '';
            
    $str explode("\n"$str);
            foreach(
    $str as $s){
                
    $res .= '<b>'.$s.'</b>';
            }
            return 
    $res;
      } 
    Also you are using this
    PHP Code:
    $body = ($HTTP_POST_VARS['body']); 
    which is deprecated. You should be using
    PHP Code:
    $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.
    Last edited by _Aerospace_Eng_; 03-12-2008 at 03:43 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Change that line to:
    PHP Code:
    $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?

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Dale_G View Post
    I'm afraid it's returning an error.

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

    Line 221 is

    Code:
                $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,

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


    best regards
    Last edited by oesxyl; 03-12-2008 at 03:47 AM.

  • #7
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    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.
    PHP Code:
    function convtobr($str)  {
            
    $res '';
            
    $str explode("\n"$str);
            foreach(
    $str as $s){
                
    $res .= '<b>'.$s.'</b>';
            }
            return 
    $res;
      } 
    Also you are using this
    PHP Code:
    $body = ($HTTP_POST_VARS['body']); 
    which is deprecated. You should be using
    PHP Code:
    $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.

  • #8
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    Change that line to:
    PHP Code:
    $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?

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Dale_G View Post
    I don't see any difference between your line of code and my current line of code?
    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
    Last edited by oesxyl; 03-12-2008 at 06:02 PM.

  • #10
    Banned
    Join Date
    Feb 2008
    Location
    Winnipeg, Canada
    Posts
    396
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Use \r\n instead of \n

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Andrew Johnson View Post
    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

  • #12
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    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
    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.

    Code:
    	/*
    	+-------------------------------------------------------
    	| 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

    Code:
    One.
    
    Two.
    Into:

    Code:
    <b>One.
    </b><b>
    </b><b>Two.</b>
    I'm looking for it to simply be:

    Code:
    <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.

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    function convtobr($str)  {
            
    $res '';
            
    $str explode("\n"$str);
            foreach(
    $str as $s){
                if(
    strlen($s) > 0)
                   
    $res .= '<b>'.$s.'</b>';
            }
            return 
    $res;
      } 
    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •