Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP字符串拆分和合并_Php_String_Yii_Split - Fatal编程技术网

PHP字符串拆分和合并

PHP字符串拆分和合并,php,string,yii,split,Php,String,Yii,Split,我有一根绳子:这是[房子/汽车]里的[男孩/女孩] 我想把它分成4个字符串: 这是一个住在房子里的男孩 这是一个住在房子里的女孩 这是一个坐在车里的男孩 这是一个坐在车里的女孩 我该怎么做?谢谢。我希望这会对你有所帮助 $string = 'This is a [boy/girl] in a [house/car]'; preg_match_all('/\[(.+?)\]/', $string, $matches); $find_array = $matches[0]; foreach($mat

我有一根绳子:这是[房子/汽车]里的[男孩/女孩] 我想把它分成4个字符串:

这是一个住在房子里的男孩 这是一个住在房子里的女孩 这是一个坐在车里的男孩 这是一个坐在车里的女孩
我该怎么做?谢谢。

我希望这会对你有所帮助

$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);
//输出


我在你的问题中没有看到任何PHP代码。你要找的是所谓的排列。在优先使用正则表达式匹配方括号中的替代项之后,您需要一个循环或递归函数。还有一个PHP示例。。如果您只是来复制粘贴,请使用搜索。我想要一个解决方案。不是代码。谢谢,非常感谢。我在寻找全局情况,所以我需要循环,但我不知道循环有多少步。对不起,我的英语不好
[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