字符串怎么按照"."和"?"同时切割
时间: 2023-08-14 21:09:19 浏览: 123
java代码-使用java解决切割字符串的源代码
在PHP中,你可以使用explode()函数来按照指定的分隔符切割字符串。例如,如果你想按照"."和"?"同时切割字符串,你可以这样做:
```php
$str = "a.string.separated.by.question.mark";
$splits = explode(".", $str);
$result = \[\];
foreach ($splits as $split) {
$result = array_merge($result, explode("?", $split));
}
print_r($result);
```
这将输出以下结果:
```
Array
(
\[0\] => a
\[1\] => string
\[2\] => separated
\[3\] => by
\[4\] => question
\[5\] => mark
)
```
请注意,我们首先使用explode()函数按照"."切割字符串,然后再使用foreach循环遍历每个切割后的子字符串,并使用explode()函数按照"?"切割每个子字符串。最后,我们将所有的切割结果合并到一个数组中。
#### 引用[.reference_title]
- *1* *4* [php根据字符串分割字符串_如何在PHP中按字符串分割字符串?](https://blog.csdn.net/cuma2369/article/details/107666597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *5* [把字符串按照某几个字符切割成数组](https://blog.csdn.net/weixin_43816287/article/details/117016799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [【Groovy】Groovy 方法调用 ( 字符串切割 | 使用 Java 语法切割字符串 | 使用 Groovy 语法切割字符串直接为...](https://blog.csdn.net/han1202012/article/details/122283009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文