Andrew R Posted May 27, 2007 Share Posted May 27, 2007 Hi how do I only display a limited amount of characters in php? For example: <? echo $row_users[‘name’]; ?> Name = Robb_Bob. How would I only display only display Robb from the database? All names in the database are separated by _ . Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/ Share on other sites More sharing options...
trq Posted May 27, 2007 Share Posted May 27, 2007 <?php echo substr($row_users[‘name’],0,4); ?> Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/#findComment-262656 Share on other sites More sharing options...
kenrbnsn Posted May 27, 2007 Share Posted May 27, 2007 That won't work, since the quotes characters are wrong. You should be using normal single quotes ' not "smart" quotes ‘’ Also if the first string is longer than 4 characters, a better way would be to use the explode() function: <?php $tmp = explode('_',$row['name']); echo $tmp[0]; ?> Ken Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/#findComment-262657 Share on other sites More sharing options...
trq Posted May 27, 2007 Share Posted May 27, 2007 I just re-read your post... are you saying you always want to display the part before the _ ? No matter what the length? That would be easiest achieved by.... <?php $names = explode('_',$row_users[‘name’]); echo $names[0]; ?> Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/#findComment-262659 Share on other sites More sharing options...
Andrew R Posted May 27, 2007 Author Share Posted May 27, 2007 I just re-read your post... are you saying you always want to display the part before the _ ? No matter what the length? Yeah most names in the database are four characters long but some are longer so I want to display everything before the _. How would have add something that this into the above code $names = addslashes($riga[2]); instead of the <? echo $row_users[‘name’]; ?> Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/#findComment-262663 Share on other sites More sharing options...
Andrew R Posted May 27, 2007 Author Share Posted May 27, 2007 Sorry to bring this topic up again but how would I display everything after the _ if the name was Robb_Bob how would I display Bob. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/#findComment-262773 Share on other sites More sharing options...
gabeg Posted May 27, 2007 Share Posted May 27, 2007 Sorry to bring this topic up again but how would I display everything after the _ if the name was Robb_Bob how would I display Bob. Thanks for your help. $var = "Robb_Bob"; $x = explode("_",$var); echo $x[1]; //use print_r to display everything in the array, so you can see what explode does //print_r($x); Link to comment https://forums.phpfreaks.com/topic/53168-solved-displaying-the-first-four-characters-in-php/#findComment-262779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.