jacko_162 Posted August 14, 2011 Share Posted August 14, 2011 im trying to use strpos to find the location after 15 characters, then i need to echo the "$data["content"] limiting it to the first white space after 15 chars, and adding the trailing dots. the echo only shows "..." not the content? where am i going wrong??? // first find the first space after 15 characters, this should be the end of a word $offset = strpos($data["content"], "", 15) ; //then put into variable $shorter_list = substr($data["content"],0,$offset); //then echo the variable with trailing dots echo $shorter_list."..."; Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/ Share on other sites More sharing options...
jcbones Posted August 14, 2011 Share Posted August 14, 2011 Got to include some whitespace in there: $offset = strpos($data["content"], ' ', 15) ; Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257284 Share on other sites More sharing options...
jacko_162 Posted August 14, 2011 Author Share Posted August 14, 2011 still no joy =( Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257424 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 His should work. More complete $str = 'This is a long string we want broken after 15 characters'; echo substr($str,0,strpos($str,' ',15)).'...'; Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257426 Share on other sites More sharing options...
jacko_162 Posted August 14, 2011 Author Share Posted August 14, 2011 even as 1 string its still only echoing the "..." hmmmm is this an old php function? Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257436 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 What? Copy n Paste my example! It works Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257455 Share on other sites More sharing options...
jacko_162 Posted August 14, 2011 Author Share Posted August 14, 2011 If i use the below i just get "..." <?php $results = mysql_query("SELECT * FROM $table3 ORDER BY id ASC LIMIT $page, $limit"); while ($data = mysql_fetch_array($results)) $str = $data["content"]; echo substr($str,0,strpos($str,' ',15)).'...'; } ?> but if i just echo $data["content"]; then i get the full content field??! Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257459 Share on other sites More sharing options...
Clarkeez Posted August 14, 2011 Share Posted August 14, 2011 Hey man, try using the function I use for this.. <?php $string = 'blah blah' echo short_str($string, 'maxnumberofcharacters'); function short_str($str, $in) { $strlen = strlen($str); if($strlen > $in): $cutamount = $in-$strlen-3; $out = substr($str, 0, $cutamount); return $out.'...'; elseif($strlen <= $in): return $str; endif; } ?> Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257464 Share on other sites More sharing options...
jacko_162 Posted August 14, 2011 Author Share Posted August 14, 2011 ok i decide to use; echo substr($data["content"], 0, 25).'..'; it works fine for limiting the content, problem now is the content now may contain html stuff... how can i strip all html out of it before echo?? if you understand what i mean, its a simple news page with a wysywig input that stores it in db, i then need to echo the first 25 characters of it for example. Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257470 Share on other sites More sharing options...
jcbones Posted August 14, 2011 Share Posted August 14, 2011 Try: <?php $results = mysql_query("SELECT * FROM $table3 ORDER BY id ASC LIMIT $page, $limit"); while ($data = mysql_fetch_array($results)) $str = $data["content"]; echo (strlen($str) > 15) ? substr($str,0,strpos($str,' ',15)).'...' : $str; } ?> Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257474 Share on other sites More sharing options...
jacko_162 Posted August 15, 2011 Author Share Posted August 15, 2011 Try: <?php $results = mysql_query("SELECT * FROM $table3 ORDER BY id ASC LIMIT $page, $limit"); while ($data = mysql_fetch_array($results)) $str = $data["content"]; echo (strlen($str) > 15) ? substr($str,0,strpos($str,' ',15)).'...' : $str; } ?> Cheers buddy, you're code with a little addition = happy man <?php $str = $data["content"]; $str2 = strip_tags($str); echo (strlen($str2) > 35) ? substr($str2,0,strpos($str2,' ',35)).'........' : $str2; ?> Link to comment https://forums.phpfreaks.com/topic/244777-strpos-echo-not-working/#findComment-1257481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.