分解标题并完全删除零件


Exploding a title and removing a part completly

当前,当我分解标题示例艺术家-示例歌曲名称[CDQ]时,我可以使用下面的代码获得实例艺术家范例歌曲名称,而不会出现任何问题。

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = $str[1];

现在我正试图删除标题的[CDQ]部分,但它必须是括号和括号内的内容,因为有时标题包括[iTunes]

我目前正在使用WordPress。

使用preg_replace替换方括号中的字符串。

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = preg_replace('#'[[a-zA-Z].*']#','',$str[1]);

它将匹配从aZ的所有字母,包括括号。

在不太可能的情况下,括号内可能有数字,您可以更改为:

$song = preg_replace('#'[[a-zA-Z0-9].*']#','',$str[1]);

您可以使用以下正则表达式来实现您想要的所有功能:

preg_match_all('/([^-]*)-([^[]*)/s', $subject, $result, REG_PATTERN_ORDER);
$artist = trim($result[1]);
$songtitle= trim($result[2]);