gpt4 book ai didi

php - php中的子字符串无论单词是字符串中的第一个还是最后一个

转载 作者:搜寻专家 更新时间:2023-10-31 21:26:32 25 4
gpt4 key购买 nike

我有这段 PHP 可以在字符串中查找事件名称,然后在出现事件名称时使用 substr。

 $chars = array("æ", "ø", "å");
$replace = array("ae", "oe", "aa");

$dash = strpos(utf8_decode($campaign["campaign_name"]), "Studiepakke");
$decoded_name = utf8_decode($campaign["campaign_name"]);
$id_name = str_replace($chars, $replace, trim(strtolower(substr($decoded_name, 0, ($dash-2)))));

$lpage_pos = strripos($id_name, $_GET["lpage"]);
$short_name = strtolower(utf8_decode($campaign["adset_name"]));
$dash_pos = strpos($short_name, " - ");

$campaign["campaign_name"] 可以看起来像“aalborg - studiepakke”,但也可以是“studiepakke - aalborg”

我如何使用 substr 和 strpos,这样“Studiepakke”是在字符串的开头还是结尾并不重要。我的最终结果永远是

“奥尔堡”。

最佳答案

如果我做对了,你可能想在破折号所在的位置拆分字符串。为此,使用

$substrings = explode(" - ", $string);

这会给你一个子串数组,然后你可以检查哪个是搜索到的。

您还可以使用字符串替换将不需要的部分替换为空字符串。

str_replace()
str_ireplace() // case insensitive

关于php - php中的子字符串无论单词是字符串中的第一个还是最后一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35195974/

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