使用标记和php从字符串中剥离文本

使用标记和php从字符串中剥离文本,php,Php,如何使用php去除标题中“-”(包括-)后面的代码 Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services 试函数 或者,您可以使用“分解”功能并抓取所需零件 $parts = explode('-', $title); $title = current($parts); //first $title = end($parts); //last $title = $par

如何使用php去除标题中“-”(包括-)后面的代码

Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services
试函数

或者,您可以使用“分解”功能并抓取所需零件

$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";
$s = explode("-",$str);
print $s[0];
$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;
$str = "Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services";
$s = explode("-",$str);
print $s[0];