doujia1904 2016-10-11 14:17
浏览 108
已采纳

在PHP中的字母之间替换分号

I have the following issue, I need to replace all semicolons inside a string which appear only between letters with a slash. The issues is that I also have html entities with semicolons and str_replace messes them up.

Sample string:

Category A > Subcategory A;Category A > Subcategory B;Category B > Subcategory C

Desired output:

Category A > Subcategory A/Category A > Subcategory B/Category B > Subcategory C
  • 写回答

2条回答 默认 最新

  • doumou3883 2016-10-11 14:28
    关注

    To replace semicolon between letter only, you can use regex lookahead and lookbehind.

    $str = "Category A > Subcategory A;Category A > Subcategory B;Category B > Subcategory C";
    $str = preg_replace("/(?<!\s);(?!\s)/", "/", $str);
    echo $str;
    

    or if your html entities is only &gt; then you could use code below

    $str = "Category A &gt; Subcategory A;Category A &gt; Subcategory B;Category B &gt; Subcategory C";
    $str = preg_replace("/(?<!\&gt);/", "/", $str);
    echo $str;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有没有帮写代码做实验仿真的
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题