imimin Posted June 14, 2009 Share Posted June 14, 2009 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 More sharing options...
joel24 Posted June 14, 2009 Share Posted June 14, 2009 use substr... http://au.php.net/substr i.e. substr($item_desc, 0, 15) would return the first 15 characters. Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855423 Share on other sites More sharing options...
imimin Posted June 14, 2009 Author Share Posted June 14, 2009 Thank you! May I ask what the parameter '0' is for? Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855430 Share on other sites More sharing options...
taquitosensei Posted June 14, 2009 Share Posted June 14, 2009 starting at zero. The first character. Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855432 Share on other sites More sharing options...
imimin Posted June 14, 2009 Author Share Posted June 14, 2009 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 Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855440 Share on other sites More sharing options...
joel24 Posted June 14, 2009 Share Posted June 14, 2009 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>"; Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855443 Share on other sites More sharing options...
imimin Posted June 14, 2009 Author Share Posted June 14, 2009 Thank you again! That did the trick! What does the ". ." do? Can it only be used in this situation? Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855608 Share on other sites More sharing options...
wildteen88 Posted June 14, 2009 Share Posted June 14, 2009 The period symbol ( . ) is for concatenation. Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855612 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 Think of it as addition for strings. 8 + 8 = 16 '8' . '8' = 88 Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855656 Share on other sites More sharing options...
imimin Posted June 14, 2009 Author Share Posted June 14, 2009 I don't see what I am concatenating in the above code? Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855685 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 ...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>"; Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855687 Share on other sites More sharing options...
thebadbad Posted June 14, 2009 Share Posted June 14, 2009 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 Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855688 Share on other sites More sharing options...
imimin Posted June 14, 2009 Author Share Posted June 14, 2009 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! Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855743 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 well he also put the substr back in quotes which is what his problem was to begin with... as mentioned before in this thread: functions do not get parsed inside quotes! Link to comment https://forums.phpfreaks.com/topic/162105-truncating-a-text-field/#findComment-855753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.