Page 1 of 1

Limit characters in a string

Posted: Wed Jan 31, 2007 9:17 pm
by Jim_Bo
Hi,

Im trying to create a function to limit the characters within a string, then keep going untill it finds the first space in the string and echo the results.

Code: Select all

function LimitString() {
	$limit=14;
	$value = substr($value, $limit, 1);
if($value != " "){
	while($value != " "){
		$i = 1;
		$limit = $limit + $i;
		$value = substr($value, $limit, 1);
 }
}
	$value = substr($value, 0, $limit);
	return $value; 
}
Thats what I have so far, but makes the browser output page can not be displayed.

Am I on the right track?


Thanks

Posted: Wed Jan 31, 2007 9:28 pm
by superdezign
You set the value of $value a hell of a lot of times.

What you're attempting to return is multiple characters in a string of only one.


Maybe, to simplify, have your function take the string as a parameter.. maybe $sString, then traverse THAT string using $value = substr($sString, $limit, 1).

Return substr($sString, 0, $limit), and it should work.

Posted: Wed Jan 31, 2007 9:45 pm
by Jim_Bo
Hi,

Do u mean something like:

Code: Select all

function LimitString() {
	$limit = 14;
	$value = substr($sString, $limit, 1);
if($value != " "){
	while($value != " "){
		$i = 1;
		$limit = $limit + $i;
		$value = substr($sString, $limit, 1);
 }
}
	return substr($sString, 0, $limit);
}

That stills returns "Page can not be displayed"


Thanks

Posted: Wed Jan 31, 2007 9:47 pm
by superdezign
take the string as a parameter
More like

Code: Select all

function LimitString($sString) {