Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Php - breaking paragraphs?

Featured Replies

Hi All

 

Im working on a news section for my example site

 

If text is entered, I want to break it up by using parargraphs - Looks fine before I submit in box, but when the text is displayed I just get one big chunk of text

 

eg: I type / want:

 

Bla bla bla bla bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla.

 

Bla bla bla bla bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla.

 

Bla bla bla bla bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla.

 

 

I get:

 

Bla bla bla bla bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla.Bla bla bla bla bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla.Bla bla bla bla bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla.

 

is there a work around for this? hope it makes sense?

 

 

mycode

 

<?php 

session_name ('YourVisitID');
session_start(); // Start the session.

// If no session value is present, redirect the user.
if (!isset($_SESSION['user_id'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
	$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/db_login.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}


ini_set("memory_limit","38M");

/************************** change to match your connection method ********************/ 

require_once ('../mysql_connect.php'); // Connect to the db. 

/*************************** THE FUNCTION BIT **************************************/ 
function resize($name, $newname){ 


$size = 100; // the thumbnail height 

 $filedir = 'uploads/'; // the directory for the original image 
 $thumbdir = 'uploads/'; // the directory for the thumbnail image 
 $prefix = 'thumbnail_'; // the prefix to be added to the original name 

 $maxfile = '8000000'; 
 $mode = '0777'; 

 $userfile_name = $_FILES[$name]['name']; 
 $userfile_tmp = $_FILES[$name]['tmp_name']; 

	 $prod_img = $filedir.$userfile_name; 

	 $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 

   $returnName=$prefix.$userfile_name; // set a name to send back for the db 

	 move_uploaded_file($userfile_tmp, $prod_img); 
	 chmod ($prod_img, octdec($mode)); 

	 $sizes = getimagesize($prod_img); 

	 $aspect_ratio = $sizes[1]/$sizes[0]; 

	 if ($sizes[1] <= $size) 
	 { 
		 $new_width = $sizes[0]; 
		 $new_height = $sizes[1]; 
	 }else{ 
		 $new_height = $size; 
		 $new_width = abs($new_height/$aspect_ratio); 
	 } 

	 $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image:' . $name); 

	$srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image: '. $name); 

	ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,
ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing: '. $name); 

	ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving: '. $name); 

	 imagedestroy($destimg); 
	  unlink($prod_img);//Delete the original image 

	return $returnName; // return the name of the thumb nail 

   } 

/************************************** END OF FUNCTION ****************************/ 


if(isset($_POST['Submit'])) { ///// CHECK FORM  WHEN SUMBMITTED (if One) 

  // check that the first image has been selected if not return error 
if ($_FILES['imageOne']['name'] <> null) {	
  $nameOne = resize("imageOne","thumbOne");//resize function and return name 
   } else { 
  $whatsediterror[3]='<p class="style1"> You Need to select an image(1).</p>'; 
	$error='false'; 
  } // end if 




/**************************** CHECK FORM INPUT SECTIONS *********************************************************/ 

if(empty($_POST['heading'])){ /// CHECK HEADING FORM HAVE BEEN FILLED IN 
   $whatsediterror[1]='<p class="style1"> Please complete Heading.</p>'; 
   $error='false'; 
}  // end if 

if(empty($_POST['news'])){ /// CHECK NEWS FORM HAVE BEEN FILLED IN 
   $whatsediterror[2]='<p class="style1"> Please complete News.</p>'; 
   $error='false'; 


} else { 

/**************** NEED TO PLACE A PROPER E-MAIL FORMAT CHECK HERE *********************************/ 
} // end if 

/**************************** ALL PARTS OF FORM CHECKED *****************************************************/ 


  if (empty($error)){ // IF THERE ARE NO ERRORS THE CARRY ON 

	 // place uploaded thumbnail images into variables for db 
	 $imageOne=$nameOne; 



	 // obtain the input form details into variables for the db 
	 $heading=$_POST['heading']; 
	 $news=$_POST['news']; 


	 $query = "INSERT INTO newspage (heading, news, registration_date, file1) VALUES ('$heading', 
'$news', NOW(), '$imageOne')";  
	  $result = @mysql_query ($query); // Run the query. 

		 if (mysql_affected_rows() == 0) { // A record was added so what next (if three) 
		 echo'<p>There seems to be an error.</p>'; 
		 echo'<p>ERROR: The insertion into the db</p>'; 
			exit; 
		 } // end if three 

	 /****************** ALL WENT OK AND ENTERED INTO DATABASE DO SOMETHING ***************************/ 
   $imagesize='height="100" width="100"';
   $imagesizeuploaded='height="1100" width="100"';
   $alignright='right';




  // display the heading,news and image 
  echo '<div id="maincentredbig">Add News</div><div id="maincentredbig1"><p class="alertHd" align="center">Latest News has been added.</p>';	
  echo'<p class="alertHd" align="left">' .$heading .'</p>'; 
  echo'<p class="alertHd" align="left"><img ' . $imagesize .' src="uploads/'.$imageOne .'" align="'
.$alignright .'" />' .$news .'</p></div>';

  exit; 

	 } // end if two 

} // end if One 

/****************** DISPLAY FORM WITH ERROR OR NOT ***************************/ 
?> 




<div id="maincentredbig">Add News</div> 
<br /> 
<div id="maincentredbigleft"><br /> 
<form action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="post"> 
<span>File 1 to upload : <input type="file" name="imageOne" /><br /></span><br /> 
<? echo $whatsediterror[3] ?><br /> 
<span>Heading: <input type="text" name="heading" <?php if (isset($_POST['heading'])) {?> value="<?php echo $_POST['heading'] ?>"<?php }?> size="53" /><span class="smallerrormessage">Required</span><br /><? echo $whatsediterror[1] ?></span><br /> 
<span>News: <textarea name="news" cols="52" rows="6"><?php if (isset($_POST['news'])) {?><?php echo $_POST['news'] ?><?php }?></textarea><span class="smallerrormessage">Required</span><br /><? echo $whatsediterror[2] ?></span><br /> 
<input type="Submit" name="Submit" value="Submit"> 
</form> 
</div>

  • Author

Thanks for that Brandon! Ive wrapped in <pre> tags but the text is a different format and over laps image? see attached?

 

Thomas Thomassen, how would I get the text to be wrapped in <p> when the text inserted is just plantext in a box?

 

echo'<p class="alertHd" align="left"><img ' . $imagesize .' src="uploads/'.$imageOne .'" align="'.$alignright .'" /><pre>' .$news .'</pre></p></div>';

post-1721-1188324928_thumb.png

How do you want to break it up? Do you want it to keep it's paragraphs or sentences? You need to do a string replace when it's submitted. Replace newlines with html tags. ie

 

$string = str_replace("\n", '<br />', $string);

 

if you want to wrap it in p tags.

 

$string = '<p>'.str_replace("\n\n", '</p><p>', $string).'</p>';

If you still want to use the <pre> tags, you just have to change the styling in your stylesheet to whatever you want. I agree that the default styling is not usually very pretty to look at.

How is the text formatted?

 

Are the paragraphs broken by new line chars ( \n, \n\t ) ?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.