Php 分解标题并完全删除零件

Php 分解标题并完全删除零件,php,wordpress,Php,Wordpress,目前,当我分解标题示例艺术家-示例歌曲名称[CDQ]时,我能够使用下面的代码毫无问题地获得示例艺术家和示例歌曲名称 $title = htmlentities(get_the_title ()); $str = explode ("–", $title); $artist = $str[0]; $song = $str[1]; 现在我试图删除标题中的[CDQ]部分,但它必须是括号和括号内的内容,因为有时标题包括类似[iTunes]的内容 我目前正在使用WordPre

目前,当我分解标题示例艺术家-示例歌曲名称[CDQ]时,我能够使用下面的代码毫无问题地获得示例艺术家示例歌曲名称

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = $str[1];
现在我试图删除标题中的[CDQ]部分,但它必须是括号和括号内的内容,因为有时标题包括类似[iTunes]的内容


我目前正在使用WordPress进行此操作。

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

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

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

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

使用
preg\u replace
替换方括号中的字符串

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
它将匹配从
a
Z
的所有字母,包括括号

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
如果括号内可能有数字,您可以更改为:

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

使用
preg\u replace
替换方括号中的字符串

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
它将匹配从
a
Z
的所有字母,包括括号

$title = htmlentities(get_the_title ());
$str = explode ("–", $title);
$artist = $str[0];
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
如果括号内可能有数字,您可以更改为:

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

这也没用。。或者我不明白你到底想说什么。。当我更改代码以符合您所说的内容时,它只是给了我一个错误字符串是什么?我没有那些弦……哦,对不起。它应该是$title而不是$subject。如果它抱怨没有定义$result,则在上面的代码之前添加$result=array(),即使这只会给出一个警告。。或者我不明白你到底想说什么。。当我更改代码以符合您所说的内容时,它只是给了我一个错误字符串是什么?我没有那些弦……哦,对不起。它应该是$title而不是$subject。如果它抱怨没有定义$result,请在上面的代码之前添加$result=array(),尽管这只会给出一个警告。非常欢迎!非常慷慨的赞美:)非常欢迎!并且非常慷慨地赞美:)