PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kyoru
Forum Commoner
Posts: 26 Joined: Mon Feb 13, 2006 9:35 pm
Post
by kyoru » Tue Feb 14, 2006 5:13 pm
Code: Select all
$Length = 5;
$Str = 'asqvasyhafveryf';
if(strlen($Str) > $Length){
preg_match('/[a-zA-Z0-9]{0,'.$Length.'}/', $Str, $M);
$Output = $M . '...';
}
I tried using this code, but it did not work, so i tried figuring out what it really does. I took out '. & .', what do the slashes mean? and when i print the output it prints out the word "Array", so im guessing $M is the output variable. Any help would be great thanks
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Feb 14, 2006 5:25 pm
check out the documentation on the function:
preg_match()
An explaination of what everything means can be found through the stickies in the
Regex board .
var_export() ,
print_r() and
var_dump() may be of interest too.
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Tue Feb 14, 2006 5:29 pm
substr would be good for this
khaki_monster
Forum Commoner
Posts: 73 Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:
Post
by khaki_monster » Tue Feb 14, 2006 8:03 pm
hi!
this is one of my functions particularly to trim exceeding lenght of a string.
hope this may help.
Code: Select all
function charslen($str){
// 42 characters is supposed to return if TRUE
if(strlen($str) > 42){
$chunk = trim(substr($str, 1, 42));
$str = $chunk.'...';
return($str);
}else{
return($str);
}
}
/*
example:
$var = 'sdfsd sdf sdfsdfsdfsdfsdfsdfs sdfsdf sdf'';
echo charslen($var);
*/
cheerz!
kyoru
Forum Commoner
Posts: 26 Joined: Mon Feb 13, 2006 9:35 pm
Post
by kyoru » Wed Feb 15, 2006 12:43 am
thanks alot guys, preg_match was a big headache for me but substr was perfect!
khaki_monster
Forum Commoner
Posts: 73 Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:
Post
by khaki_monster » Wed Feb 15, 2006 5:44 am
glad we've help yah...
i dont like regex but basically its important also to learn some regex.
cheerz!