Hi,

I am saving data to text filed i want to display first 50 characters from that text field.

I tried it with left() function with my sql query and also tried it with substr() function in php code also.but i am getting a problem it shows first 50 characters but in the text field data comes from rich text editor.

just look at my text filed data..

<p style="text-align: left;"><strong>પે<span style="color: #3366ff;"><span style="background-color: #ffff99;">લિ લાઇન</span></span></strong></p>
<p style="text-align: left;"><span style="color: #ff0000;">બિજિ લાઇન</span>લાઇન</p>
<p style="text-align: left;"><img src="../upload/17a9440cb578f805e99974fd7e003653.jpg" alt="t" width="800" height="600" /></p>
<p style="text-align: left;">&nbsp;</p>
<p style="text-align: left;">&nbsp;</p>
<p style="text-align: left;">&nbsp;</p>
<p style="text-align: left;">&nbsp;</p>
<p style="text-align: left;">&nbsp;</p>
<p style="text-align: left;">ચોથિ લાઇન</p>
<p style="text-align: left;">પાંચ્મિ લાઇન</p>

so if i use any of above function then i didn't got any formatting and also it breaks formatting.

when i called first 50 characters it only shows

પે

without formatting.

Dose any one know how to display only characters without counting the formatting coding and also with formatting.

thanks

Have you tried using PHP's strip_tags() function?

http://se2.php.net/strip_tags

You might also send the text through another function you create yourself to remove things like "&nbsp;"

Hi,

Thanks for reply.

no i didn't try it.but after you told i tried it something like this.

<?php 
  $news = strip_tags($row['apal_news']);
   $snews = substr($news,0,50);
echo $news;
 ?>

First i use strip_tags and then i use substr() it working. its shows first 50 characters.

But now problem is that it removed all formatting.it didn't shows this 50 characters with formatting. :(

can you please tell me how to show this characters with formatting.?

Thanks

then use substr only. then the formatting will stay in place. Dont strip the tags

and here is some basic substr info for reference use.

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f

?>

just remember one thing, lets say you strip the tags after the first 15 characters. And if you dont strip the tags, the html code will count as characters. Secondly, if, lets say, your sting is "bold" and you use substr before the tag is closed, everything else will be bold.

For example, if you used italic, and if you use substr for the first 5 characters, this would be the result

<i>Hello. My name is peter. How are you today</i>

The result will turn like this :
Hello. My name is
AND THE REST OF YOUR PAGE WILL BE ITALIC TOOOOOOO!!!!!

and all the text below that, will be italic too. So unless you are for sure that the formatting tags will be closed before the substr, it might be good advice NOT to use formatting in your first "50" characters.

Understand what I am trying to tell you?

Member Avatar for Zagga

One option might be to strip out all the tags (and formatting), cut the string down to the size you want, then add new formatting and style. This will only be useful if the formatting is always the same though.

Zagga

ditto @ Zagga

Member Avatar for diafol

This depends on your RTE (rich text editor). I assume it has a HTML view in addition to WYSIWYG. Simply have a second hidden RTE (hidden by CSS) and use programmatic JS to copy and paste the text from one to the next. The first RTE contains the full text, with the second containing the truncated version (hopefully with the underlying html intact).

This will depend on which RTE you're using.

Thanks all of you for help me.

@ sallecpt :

yes i tried it with substr() too without using strip_tags() but as u told it converted whole page into bold well you are rite that its batter to not use first 50 characters.but here its must.because its for news and this is on all news listing page.so on listing page we can't show whole news.so we have to show some of the starting part of that news.

I hope you can understand my problem. :)

_________________________________

@ Zagga

may b it will work.i will try it will back to u. :)
__________________________________

@ ardav

yes it something working like that.i am using the WYSIWYG editor which specially developed for indian languages.here is the link of that editor.

http://service.vishalon.net/pramukhtypepad.htm

I given you link may be after check this editor you got any idea.
i have to use this because the news site is in Gujarati.

___________

Thanks again all of you. :)

@ ardav

your given link is for smarty plugin.but i m not using smarty then it will work for me or not ?

Member Avatar for diafol

Yes it'll work - just use somethign like this:

$p1='<p><span class="firstline">Mae Hen Wlad Fy Nhadau yn Annwyl i Mi,</span> Gwlad Beirdd a Chantorion, Enwogion o Fri, Ei Gwrol </p>';

echo html_substr($p1,20);

Notice I changed the name of the function to:

function html_substr($string, $length) {

The above code gives:

Mae Hen Wlad Fy

As you can see the word 'Nhadau' should be truncated - but the script is clever - it looks for the previous space so that the last word isn't cut off.

I did notice one problem - if the first word in the phrase is larger than the $length variable, the first word is ignored and the output shows the remainder (the whole lot!) of the string. If you're using 50 chars - this should be a problem.

can you please tell me what should i wrote in

function html_substr($string, $length) {
Member Avatar for diafol

This code was taken from http://www.smarty.net/forums/viewtopic.php?t=533
I apologise to the OP of that site for printing out the code here.

<?php 

function html_substr($string, $length) 
{ 
if( !empty( $string ) && $length>0 ) { 
$isText = true; 
$ret = ""; 
$i = 0; 

$currentChar = ""; 
$lastSpacePosition = -1; 
$lastChar = ""; 

$tagsArray = array(); 
$currentTag = ""; 
$tagLevel = 0; 

$noTagLength = strlen( strip_tags( $string ) ); 

// Parser loop 
for( $j=0; $j<strlen( $string ); $j++ ) { 

$currentChar = substr( $string, $j, 1 ); 
$ret .= $currentChar; 

// Lesser than event 
if( $currentChar == "<") $isText = false; 

// Character handler 
if( $isText ) { 

// Memorize last space position 
if( $currentChar == " " ) { $lastSpacePosition = $j; } 
else { $lastChar = $currentChar; } 

$i++; 
} else { 
$currentTag .= $currentChar; 
} 

// Greater than event 
if( $currentChar == ">" ) { 
$isText = true; 

// Opening tag handler 
if( ( strpos( $currentTag, "<" ) !== FALSE ) && 
( strpos( $currentTag, "/>" ) === FALSE ) && 
( strpos( $currentTag, "</") === FALSE ) ) { 

// Tag has attribute(s) 
if( strpos( $currentTag, " " ) !== FALSE ) { 
$currentTag = substr( $currentTag, 1, strpos( $currentTag, " " ) - 1 ); 
} else { 
// Tag doesn't have attribute(s) 
$currentTag = substr( $currentTag, 1, -1 ); 
} 

array_push( $tagsArray, $currentTag ); 

} else if( strpos( $currentTag, "</" ) !== FALSE ) { 

array_pop( $tagsArray ); 
} 

$currentTag = ""; 
} 

if( $i >= $length) { 
break; 
} 
} 

// Cut HTML string at last space position 
if( $length < $noTagLength ) { 
if( $lastSpacePosition != -1 ) { 
$ret = substr( $string, 0, $lastSpacePosition ); 
} else { 
$ret = substr( $string, $j ); 
} 
} 

// Close broken XHTML elements 
while( sizeof( $tagsArray ) != 0 ) { 
$aTag = array_pop( $tagsArray ); 
$ret .= "</" . $aTag . ">\n"; 
} 

} else { 
$ret = ""; 
} 

return( $ret ); 
} 

?>

And then call it like my previous example.

This code was taken from http://www.smarty.net/forums/viewtopic.php?t=533
I apologise to the OP of that site for printing out the code here.

Thank you for posting code here for me. :)

and Thank you for solve my problem. It works for me. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.