nicuz Posted June 30, 2006 Share Posted June 30, 2006 I have a field in my database called "description" (type:longtext) and I would like to retreive the first let's say 13 characters. What wold the select query be? Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/ Share on other sites More sharing options...
redarrow Posted June 30, 2006 Share Posted June 30, 2006 If you mean the first 13 entrys use limit 13 Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51057 Share on other sites More sharing options...
nicuz Posted June 30, 2006 Author Share Posted June 30, 2006 No. The first 13 characters.Eg: in the field I have stored: I love php and I love programming.What I want is to select the first 13 char. which are: I love php an Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51064 Share on other sites More sharing options...
redarrow Posted June 30, 2006 Share Posted June 30, 2006 $text = "$description"; echo ShortenString($text); function ShortenString($text) { // Number of characters you want to display $num_chars = 100; $text = $text." "; $text = substr($text,0,$num_chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."..."; return $text; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51065 Share on other sites More sharing options...
never_rain Posted June 30, 2006 Share Posted June 30, 2006 Ok, here is the solution.$description = 'I love PHP and I love Programming';$description1 = substr($description,0,13);echo $description1; \\ the result will be first 13 characters of your string Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51067 Share on other sites More sharing options...
redarrow Posted June 30, 2006 Share Posted June 30, 2006 $text = "$description"; echo ShortenString($text); function ShortenString($text) { // Number of characters you want to display $num_chars = 100; $text = $text." "; $text = substr($text,0,$num_chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."..."; return $text; } ?> $num_chars = 100; // change to number of charecters.$text = "$description"; text = database varable. Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51068 Share on other sites More sharing options...
redarrow Posted June 30, 2006 Share Posted June 30, 2006 please look up substr okhttp://uk.php.net/substr Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51069 Share on other sites More sharing options...
never_rain Posted June 30, 2006 Share Posted June 30, 2006 I have another problem using substr() my description field is stored in database and is non english language. I used html editor to store the values. When I type any Character in the editor and dont use formatings from html editor, the values displayed corectly. But when I do Formating and apply these settings, nothing is shown. this means, when I use substr function, it has some confliction with the html stored in the database. here is an example. field name is description and the value from database is as follows[color=red]This value I copied Paste and used HTML Editor to increase / decrease size etc [/color] $description = <P class=MsoNormal dir=rtl style="MARGIN: 0cm 0cm 0pt; DIRECTION: rtl; unicode-bidi: embed; TEXT-ALIGN: justify"><SPAN lang=AR-AE style="FONT-SIZE: 14pt; FONT-FAMILY: 'Simplified Arabic'; mso-bidi-language: AR-AE">ÊÎÊÕ ÅÏÇÑÉ ÇäÈÍèË èÇäÇÓÊÔÇÑÇʬ ÇäÊê ÊÙÏ åæ ÇäÅÏÇÑÇÊ ÇäÑÆêÓêÉ ÈÇäåÙçϬ ÈÇäÅÔÑÇá èÇäÊèÌêç èÇäÊæÓêâ Èêæ ÇäÃæÔ×É ÇäåÎÊäáÉ ÇäåÑÊÈ×É ÈåÌÇäÇÊ ÇäÈÍèË èÇäÇÓÊÔÇÑÇÊ èÇäåãÊÈÇÊ èÇäèËÇÆâ èÇäæÔÑ</SPAN><SPAN lang=AR-SA style="FONT-SIZE: 14pt; FONT-FAMILY: 'Simplified Arabic'">.èÊäÙÈ ÇäÅÏÇÑÉ ÏèÑÇ åÍèÑêÇ áê ÊÍâêâ ÇäåÙçÏ äÃçÏÇáç èÊæáêÐ ÇäåçÇå ÇäÊê Ãæê×Ê Èç áê åÌÇä ÇäÇÑÊâÇÁ ÈåÓÊèé ÇäÅÏÇÑÉ ÇäÍãèåêÉ èÊÈÓê× ÅÌÑÇÁÇÊ ÇäÙåä ¬ è ÇäåÓÇçåÉ ÇäáÙÇäÉ áê ÇäÊæØêå ÇäÅÏÇÑê åæ ÎäÇä ÅÙÏÇÏ ÇäçêÇãä ÇäÊæØêåêÉ èÊâÏêå ÇäÇÓÊÔÇÑÇÊ ÇäÅÏÇÑêÉ èÅÌÑÇÁ ÇäÈÍèË èèÖÙ ÇäÍäèä ÇäåäÇÆåÉ ääåÔãäÇÊ ÇäÊê ÊÙèâ ÇäÙåä áê ÇäÃÌçÒÉ ÇäÍãèåêÉ ÇäÇÊÍÇÏêÉ èÇäåÍäêÉ .</SPAN><SPAN dir=ltr style="FONT-SIZE: 14pt; mso-bidi-font-family: 'Simplified Arabic'"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>These Values dont show any thing in the Page.Here is another record in whcih I didn't use any HTML editor functionalities. Just type the values and clicked submit. Here is how it looks.$description = <P>æåÊåæÊæå ÇÊæÇ æÊÇæ ÊÇæ ÇçÙ Ç ÇÙç ÇÚÙç ÚçÙ Ú Ù ÈÑäÇ ÑÊÇ äÇÊæ äÇçÊ Çä ÙÚä ÙÚ ÊÇääÇÙçä Ùç äÙç ÇÙç ÇÊ äÇÑ ÇÊääÇ äÊæÇ Ùç ÇÙç ÇÙç ÇÙç ä Úä ÙáÚÈÑ áÚÈÄ ÚÑ ÑÇÊäÇ ÊäÇ ÊÇäÇ äÑÚÙ äÙÚ äÙÚ È áÚÙ Êæ æÊ æ ÊÇ ÊÇÑ äÇÑ Ù ÑÙ ÑÙÚ ÑÙÚ Ñ ÙÚ ÙÚäÇ ÙäÇ ÙÚ äÇÙ äÇ ÙÚäÇ ÙÚäÇÑ ÙÚ ÈÑÚ ÄáâÄ áâ Äá ä Ñ ÊÇ ÊÇ ÊÇ äÇ ÇäÇ ÙÚ äÚÙ äÙÚ äÙÚ äÙÚ ä ÚÙä ÙÚä È âáÈê â êâêâê â êâ êËâ È ÄÈ ÄÈ Ä ä ÑÄä ÑäÇ Ñ äÇÑ äÄ áÄ âê â â È äÑ Ç Ñ ÇÑ Ç Ç äÇä Ç äÇ äÇ ä Çä Çä Ç ÚÈ â êÄâ ê âê Äâ ÄÈâ Ä âÄ âÄ â Äâ Äâ Äâ Ä âÄ â Äâ Äâ Äâ Ä âÄ åÊ æåÊ æåÊ æå Êæå Êæå ÊçÎÇ ÎçÇ Ùç ÇÙç äç äÙçÚä ÙÚçä ÙçÚä çÙä çÙä çÙ ä çÚä äÇ éÉäÇ ÇäÇ ÙÊÇäÇÑ ÙÚ ÙÚ </P>[color=red]This value is shown perfectly[/color]. What I have figured out is that there is some conflict of HTML codes used above with substr and thats why it didn't show any thing. Do you have any idea what happened wit this. Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51078 Share on other sites More sharing options...
redarrow Posted June 30, 2006 Share Posted June 30, 2006 lets get this stright you added html to the database? why?does the code work as a normal insert without html inserted.when you add data to the database you need to addslahes okaddslases($text); Quote Link to comment https://forums.phpfreaks.com/topic/13261-retreive-first-characters-from-a-field/#findComment-51086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.