我有一个在DIV中有名字的网页。有些名称很长,在某些屏幕宽度上会产生线刹。在这些情况下,我想将第一个单词(例如名字)缩短到最初的单词(例如第一个字母)并保持其余部分(前David van Macleaoderson - > D van Macleaoderson)用于较小的线刹车。
如何检测DIV中的浏览器渲染线制动然后执行缩短操作?
答案 0 :(得分:1)
You can first count the length of that div (means how many character adjust in that div) for example div adjust only 20 character
$divMaxVal = 20;
$name = "David van Macleaoderson";
$stringcnt= strlen($name); // Count the length of string i.e. 23
if($stringcnt>$divMaxVal){ // Check whether string count ($stringcnt) value is greater than $divMaxVal or not
//StringCount Value greater than $divMaxVal
$split = explode(' ', $name,2); // Explode the String in first occurance of delimeter
$firstchar = $split[0]; // =David
echo $firstchar[0].' '.$split[1]; // =D van Macleaoderson // combine the first character from David (i.e.Explode array 1st value ) and Explode array 2nd value
}else{
// if StringCount Value is not greater than $divMaxVal
echo $name;
}