How can i strip out the code after the "-" (including the -) in this 开发者_运维百科title using php?
Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services
try strtok function
$title = strtok($title, '-');
alternatively you can use explode function and grab required part.
$parts = explode('-', $title);
$title = current($parts); //first
$title = end($parts); //last
$title = $parts[0] //using index
$str = 'Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services
';
// find first occurrence of '-'
$pos = strpos($str, '-');
// if '-' found, take the substring from the beginning to the position found
$str = ($pos !== false) ? substr($str, 0, $pos) : $str;
use explode on "-" and get first element.
$str = "Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services";
$s = explode("-",$str);
print $s[0];
精彩评论