Log in

View Full Version : print out characters



boogyman
04-09-2007, 08:23 PM
Building a script for email processing, and I am having problems limiting the number of characters given for a certain field. the function will take 2 arguments, the input, and some length. it returns the string to a maximum of "length" characters.



function friendly($text, $length=1024) {
for($i=0; $i<$length; $i++) {
echo ____character____
}
}


I only want to print out 1 character at a time, until the limit is reached then it would end


so if i were to call


friendly("my name is josh", 5)
it would return "my na"

thetestingsite
04-09-2007, 08:36 PM
Why not try taking out this part (in red) in the code:



function friendly($text, $length=1024) {
for($i=0; $i<$length; $i++) {
echo ____character____
}
}


Then call the script like you have posted above.
Hope this helps.

boogyman
04-09-2007, 08:52 PM
the 1024 is a default value, incase you dont pass anything, preventing the script from erroring, but if you pass a number into the argument it gets overwritten... what I am looking for help on is actually printing out the characters 1 at a time


and yes i have tried without the value, still havent been able to get the function to print out 1 character at a time

Twey
04-09-2007, 09:30 PM
There's no need for this, the built-in substr (http://www.php.net/substr)() function will do just as well:
$tring = 'My name is Josh';
echo substr($tring, 0, 5);

djr33
04-09-2007, 10:16 PM
Twey is right, but for the original, I'd use the function strlen($str), where $str is the string being accessed, to go about the for-loop, rather than an arbitrary value which, in the best case, wastes time, or, for a long string, won't finish the string.


EDIT: I just noticed your amusing string name. //laugh.

boogyman
04-10-2007, 12:31 AM
WoW, talk about a brain cramp. thanks twey