drddx3115 2016-02-04 08:28
浏览 121
已采纳

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".

  • 写回答

2条回答 默认 最新

  • dongxu7121 2016-02-04 08:35
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退