我有这段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“是在字符串的开头还是结尾并不重要。我的最终结果永远是
“阿尔伯格”
发布于 2016-02-04 08:35:52
如果我做得对,你可能想在破折号所在的地方分割字符串。为此目的,请使用
$substrings = explode(" - ", $string);这将给您一个子字符串数组,然后您可以检查哪一个是搜索的。
还可以使用字符串替换将不需要的部分替换为空字符串。
str_replace()
str_ireplace() // case insensitive发布于 2016-02-04 08:50:44
对于您所描述的问题,我有一些不同的方法:
if(strpos($campaign["campaign_name"], "studiepakke")) //studiepakke is part of the string.或者:
if(preg_match("/(^studiepakke|studiepakke$)/i", $campaign["campaign_name"])) //studiepakke is exacly at begin or ending of the string.https://stackoverflow.com/questions/35195974
复制相似问题