Jump to content

Truncating a text field


imimin

Recommended Posts

Can someone tell me how to truncate a text field so I might echo only the first 15 characters of a text field (pulled from a MySQL DB)?

 

Example of what i am trying to do is:

 

echo "<center><b>$item_prod_name</b></center>
         <HR width=80%>
	 $item_desc
                 <BR><BR>
         $item_prod_code
         <BR>
	 <B>$item_retail</B></P>
	 <BR></TD>";

 

In this example I would like help with truncating the data that is echoed from "$item_desc" to 15 characters.

 

Thank you for your help!

Link to comment
https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/
Share on other sites

When I try this code:

 

			echo "<center><b>$item_prod_name</b></center>
		      <HR width=80%>
		      substr($item_short_desc,0,15)
		      <BR><BR>
		      $item_prod_code
		      <BR>
		      <B>$item_retail</B></P>
		      <BR></TD>";

 

(see line 3), I get the following output:

 

substr(Short item description goes right here in this space!,0,15)

 

What am I doing wrong?

 

Thanks!

 

Gary

the php won't execute a function inside quotes, only echo variables.

change it to:

 

			echo "<center><b>$item_prod_name</b></center>
		      <HR width=80%>
		      ".substr($item_short_desc,0,15)."
		      <BR><BR>
		      $item_prod_code
		      <BR>
		      <B>$item_retail</B></P>
		      <BR></TD>";

 

...so...you don't see something between two periods?

 

echo "<center><b>$item_prod_name</b></center>

              <HR width=80%>

              ".substr($item_short_desc,0,15)."

              <BR><BR>

              $item_prod_code

              <BR>

              <B>$item_retail</B></P>

              <BR></TD>";

I don't see what I am concatenating in the above code?

 

You're concatenating the string <center><b>$item_prod_name</b></center><HR width=80%>, the return of substr($item_short_desc,0,15) and the string <BR><BR>$item_prod_code<BR><B>$item_retail</B></P><BR></TD>. Hence the dots between those parts :)

Sorry, I have only ever seen one period used for concatenation.  I would have guessed to concatenate the code in question as:

 

         echo "<center><b>$item_prod_name</b></center>
               <HR width=80%>".
               "substr($item_short_desc,0,15)".
               "<BR><BR>
               $item_prod_code
               <BR>
               <B>$item_retail</B></P>
               <BR></TD>";

 

Any more input on this?  I guess I have a lot to learn!

Archived

This topic is now archived and is closed to further replies.

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