PHP - If variable's character is number then do something
Solution 1:
Try:
$name = (strlen($arr['name']) > 12) ? substr($arr['name'],0,12) . "..." : $arr['name'];
This is a conditional assignment. If $arr['name']
is greater than 12 characters, then it truncates it to 12 characters and adds "...". Otherwise, it uses the full name.
Solution 2:
if (strlen($name) > 12)
$name = substr($name, 0, 12) . '...';
https://php.net/strlen
https://php.net/substr
Solution 3:
You can do something like this:
if( strlen($arr['name']) > 12)
{
substr($arr['name'],0,12)."...";
}
So, your code will look like:
$query= mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$name= $arr['name'];
echo '</br>';
if(strlen($name) > 12)
{
substr($name,0,12) . "...";
}
}