- preg_match: documentation ( source)
- trim: documentation ( source)
- preg_split: documentation ( source)
- array_unique: documentation ( source)
<?php
$string = 'Samsung UN32EH4003 32-inch 720p 60Hz LED HDTV (Black)';
echo shorten_string($string);
function shorten_string($string,$count=5){
$arr = preg_split('/[,\ \.;]/', $string);
$keywords = array_unique($arr);
$i=0;
$rtn = '';
foreach ($keywords as $keyword){
if ((preg_match("/^[a-z0-9]/i", $keyword) ) && (strlen($keyword) > 3)){
$rtn .= $keyword.' ';
$i++;
if ($i==$count) break;
}
}
return trim($rtn);
}