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

I have this piece of PHP to find a campaign name in string and then use substr on the occurrence of the campaign name.

 $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"] can look like "aalborg - studiepakke", but it can also be "studiepakke - aalborg"

How can I use substr and strpos so it is unimportant whether "Studiepakke" is at the beginning or at the end of the string. My end result will always be

"aalborg".

If I get it right you may want to split strings where the dash is. For this purpose, use

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

This will give you an array of substrings, you can then check which one is the searched one.

You could also use string replace to replace the unwanted part with an empty string.

str_replace()
str_ireplace() // case insensitive

I have some different approaches for the problem you are describing:

if(strpos($campaign["campaign_name"], "studiepakke")) //studiepakke is part of the string.

Or:

if(preg_match("/(^studiepakke|studiepakke$)/i", $campaign["campaign_name"])) //studiepakke is exacly at begin or ending of the string.