douya6229 2014-09-25 21:02
浏览 42
已采纳

Querystring参数explode str_replace数组仅适用于1个分隔符

I am using a explode and str_replace on the get parameter of the query string URL. My goal is to split the strings by certain characters to get to the value in the string that I want. I am having issues. It should work but doesn't.

Here are two samples of links with the query strings and delimiters I'm using to str_replace.

http://computerhelpwanted.com/jobs/?occupation=analyst&position=data-analyst

as you can see the URL above parameter is position and the value is data-analyst. The delimiter is the dash -.

http://computerhelpwanted.com/jobs/?occupation=analyst&position=business+systems+analyst

and this URL above uses same parameter position and value is business+systems+analyst. The delimiter is the + sign.

The value I am trying to get from the query string is the word analyst. It is the last word after the delimiters.

Here is my code which should do the trick, but doesn't for some reason.

$last_wordPosition = str_replace(array('-', '+')," ", end(explode(" ",$position)));

It works if the delimiter is a + sign, but fails if the delimiter is a - sign.

Anyone know why?

  • 写回答

3条回答 默认 最新

  • douzi8916 2014-09-25 21:15
    关注

    You have things in the wrong order:

    $last_wordPosition = end(explode(" ", str_replace(array('-', '+'), " ", $position)));
    

    You probably want to split it up so as to not get the E_STRICT error when not passing an variable to end:

    $words = explode(" ", str_replace(array('-', '+'), " ", $position));
    echo end($words);
    

    Or something like:

    echo preg_replace('/[^+-]+(\+|-)/', '', $position);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错