gpt4 book ai didi

PHP 字符串转换为以单词开头为键的数组

转载 作者:可可西里 更新时间:2023-11-01 13:33:54 26 4
gpt4 key购买 nike

假设我有以下字符串

$str = "once in a great while a good-idea turns great";

创建数组的最佳解决方案是什么,数组键是单词开始处的字符串计数?

$str_array['0'] = "once";
$str_array['5'] = "in";
$str_array['8'] = "a";
$str_array['10'] = "great";
$str_array['16'] = "while";
$str_array['22'] = "a";
$str_array['24'] = "good-idea";
$str_array['34'] = "turns";
$str_array['40'] = "great";

最佳答案

简单如下:

str_word_count($str, 2);

str_word_count() 的作用是

str_word_count() — Return information about words used in a string

关于PHP 字符串转换为以单词开头为键的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14713206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com