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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题