我有一个字符串:“这是[房子/车]中的[男孩/女孩] 我想把它分成4个字符串:
我该怎么做?感谢。
答案 0 :(得分:1)
我希望这会对你有所帮助
$string = 'This is a [boy/girl] in a [house/car]';
preg_match_all('/\[(.+?)\]/', $string, $matches);
$find_array = $matches[0];
foreach($matches[1] as $row) {
$replace_array[] = explode("/", $row);
}
$output[] = str_replace($find_array,array($replace_array[0][0],$replace_array[1][0]),$string);
$output[] = str_replace($find_array,array($replace_array[0][1],$replace_array[1][0]),$string);
$output[] = str_replace($find_array,array($replace_array[0][0],$replace_array[1][1]),$string);
$output[] = str_replace($find_array,array($replace_array[0][1],$replace_array[1][1]),$string);
print_r($output);
//输出
[0] => This is a boy in a house [1] => This is a girl in a house [2] => This is a boy in a car [3] => This is a girl in a car