Jump to content

Recommended Posts

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

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??!

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;
}
?>

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.

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;
}
?>

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;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.