gpt4 book ai didi

Add character after each word in a string (multibyte-safe)(在字符串中的每个单词后添加字符(多字节安全))

转载 作者:bug小助手 更新时间:2023-10-28 22:17:33 24 4
gpt4 key购买 nike



How can I implode character after each word?

我怎么能在每个单词之后都内爆性格呢?



I have tried the following:

我尝试了以下几种方法:



$query = implode("* ", preg_split("/[\s]+/", $query));


but it always ignores one word. for example: test test will give me test* test, test will give me test, test test test test will give me test* test* test* test

但它总是忽略一个词。例如:测试测试将给我测试,测试将给我测试,测试将给我测试



Also tried $query = implode("*", str_split($query)); but it gives me gibberish if query is in Cyrillic characters.

我还尝试了$QUERY=INPRODE(“*”,str_plit($Query));但如果查询是西里尔字符,它会让我胡言乱语。


更多回答

Add * to the end of $query

在$QUERY的末尾添加*

Is there any problem appending "*" manually? like implode("* ", preg_split("/[\s]+/", $query))."*";

手动追加“*”有问题吗?Like Implode(“*”,preg_plit(“/[\S]+/”,$QUERY)).“*”;

优秀答案推荐

implode will join strings together and insert a "glue string" in between each item.

内爆将把串连接在一起,并在每件物品之间插入一根“胶水串”。



If you want to change each and every string, you can use array_map instead.

如果您想更改每个字符串,则可以使用ARRAY_MAP。



implode(array_map(function($item) { return $item . '* '; }, $array));


And I hope you are not doing funky stuff with a database query (considering your variable name). If you want to use variable db queries, use parametrized queries instead.

我希望你没有用数据库查询做一些奇怪的事情(考虑到你的变量名)。如果你想使用可变数据库查询,请使用参数化查询。



Another "hack" is to implode your array and then insert the missing string at the end of the resulting string:

另一个“技巧”是将数组内爆,然后在结果字符串的末尾插入缺失的字符串:



implode('* ', $array) . '* ';


Try this

尝尝这个



<?php

$str = 'test test test test test';
$str = implode("* ", explode(" ", $str))."* ";

echo $str;


Try other answers or you can also use preg_replace. But not recommended. Avoid using regex when there is some clean alternatives available.

尝试其他答案,或者您也可以使用preg_place。但不推荐。当有一些干净的替代方案可用时,避免使用正则表达式。



<?php
$query = preg_replace( "/[\s]+/", "* ", $query )."*";
echo $query;
?>


Using array_map

使用arraymap



<?php
$query = explode( " ", "test test test test test" );
$query = array_map( function( $val ) {
return trim( $val )."* ";
}, $query );
$query = implode( "", $query );
echo $query;
?>



Most directly, use preg_replace() to insert an * after each word.

最直接的方法是使用preg_place()在每个单词后面插入一个*。


\w+ will match one or more alphanumeric or underscore characters.

\K tells the function to restart the full string match -- so that no characters are removed during replacement.

\w+将匹配一个或多个字母数字或下划线字符。\K告诉函数重新开始完整的字符串匹配--这样在替换过程中就不会删除任何字符。


Code: (Demo)

代码:(演示)


$string = 'test test test test';

echo preg_replace("/\w+\K/", '*', $string);
// output: test* test* test* test*

For multibyte characters such as cyrillic, add the u pattern modifier.

对于多字节字符,如西里尔文,添加u模式修饰符。


Code: (Demo)

代码:(演示)


$string = 'слово слово слово слово';

echo preg_replace("/\w+\K/u", '*', $string);
// output: слово* слово* слово* слово*

更多回答

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