开发者

Stripping text from a string using a marker and php

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜