Jump to content

Archived

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

nicuz

Retreive first characters from a field

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
$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;
}
?>

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites
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'">.èÊäÙÈ ÇäÅÏÇÑÉ ÏèÑÇ åÍèÑêÇ áê ÊÍâêâ ÇäåÙçÏ äÃçÏÇáç èÊæáêÐ ÇäåçÇå ÇäÊê Ãæê×Ê Èç áê åÌÇä ÇäÇÑÊâÇÁ ÈåÓÊèé ÇäÅÏÇÑÉ ÇäÍãèåêÉ èÊÈÓê× ÅÌÑÇÁÇÊ ÇäÙåä ¬ è ÇäåÓÇçåÉ ÇäáÙÇäÉ áê ÇäÊæØêå ÇäÅÏÇÑê&nbsp; åæ ÎäÇä ÅÙÏÇÏ ÇäçêÇãä ÇäÊæØêåêÉ èÊâÏêå ÇäÇÓÊÔÇÑÇÊ ÇäÅÏÇÑêÉ èÅÌÑÇÁ ÇäÈÍèË èèÖÙ ÇäÍäèä ÇäåäÇÆåÉ ääåÔãäÇÊ ÇäÊê ÊÙèâ ÇäÙåä áê ÇäÃÌçÒÉ ÇäÍãèåêÉ ÇäÇÊÍÇÏêÉ èÇäåÍäêÉ .</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>æåÊåæÊæå&nbsp; ÇÊæÇ æÊÇæ ÊÇæ ÇçÙ Ç ÇÙç ÇÚÙç ÚçÙ Ú Ù ÈÑäÇ ÑÊÇ äÇÊæ äÇçÊ Çä ÙÚä ÙÚ ÊÇääÇÙçä Ùç äÙç ÇÙç ÇÊ äÇÑ ÇÊääÇ&nbsp; äÊæÇ Ùç ÇÙç ÇÙç ÇÙç ä Úä ÙáÚÈÑ áÚÈÄ ÚÑ&nbsp; ÑÇÊäÇ ÊäÇ ÊÇäÇ&nbsp; äÑÚÙ äÙÚ äÙÚ È áÚÙ Êæ æÊ æ ÊÇ ÊÇÑ äÇÑ Ù ÑÙ ÑÙÚ ÑÙÚ Ñ ÙÚ ÙÚäÇ ÙäÇ ÙÚ äÇÙ äÇ ÙÚäÇ ÙÚäÇÑ ÙÚ ÈÑÚ ÄáâÄ áâ Äá ä Ñ ÊÇ ÊÇ ÊÇ äÇ ÇäÇ ÙÚ äÚÙ äÙÚ äÙÚ äÙÚ ä ÚÙä ÙÚä È âáÈê â êâêâê â êâ êËâ&nbsp; È ÄÈ ÄÈ Ä ä ÑÄä ÑäÇ Ñ äÇÑ äÄ&nbsp; áÄ âê â â&nbsp; È äÑ Ç Ñ ÇÑ Ç Ç äÇä Ç äÇ äÇ ä Çä Çä Ç ÚÈ&nbsp; â êÄâ ê âê Äâ ÄÈâ Ä âÄ âÄ â Äâ Äâ Äâ Ä âÄ â Äâ Äâ Äâ Ä âÄ&nbsp;&nbsp;&nbsp;&nbsp; åÊ æåÊ æåÊ æå Êæå Êæå ÊçÎÇ ÎçÇ Ùç ÇÙç äç äÙçÚä ÙÚçä ÙçÚä çÙä çÙä çÙ ä çÚä&nbsp; äÇ éÉäÇ ÇäÇ ÙÊÇäÇÑ ÙÚ ÙÚ </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.

Share this post


Link to post
Share on other sites
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 ok

addslases($text);

Share this post


Link to post
Share on other sites

×

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.