dsla94915 2017-10-21 20:19
浏览 35
已采纳

如何跳过strrpos Entry

Is it possible to skip a strpos/strrpos position?

    $string = "This is a cookie 'cookie'.";
    $finder = "cookie";
    $replacement = "monster";
    if (strrpos($string, $finder) !== false)
        str_replace($finder, $replacement, $string);

I want to skip the 'cookie' and replace the plain cookie so it'll result in "This is a monster 'cookie'."

I don't have qualms with it finding 'cookie' first and then checking it (Obviously necessary to determine it shouldn't be replaced), but I want to make sure that while 'cookie' is still there, I can use the same function to find the unquoted cookie.

Alternatively, is there a function I haven't found yet (Through hours of searching) to get all indices of a particular word so I can check them all through a loop without the use of regex?

It's important that it's the index, not the word itself, as there are other checks that have to be done based on where in the string the word's located.

  • 写回答

3条回答 默认 最新

  • duandu5846 2017-10-21 22:22
    关注

    The following gives the required replacement as well as the position of the replaced word.

    $string = "This is a cookie 'cookie'."; 
    $finder = "cookie";
    $replacement = "monster";
    $p = -1; // helps get position of current word
    $position = -1; // the position of the word replaced
    $arr = explode(' ',$string);
    for($i = 0; $i < count($arr); $i += 1){
    // Find the position $p of each word and
    // Catch $position when a replacement is made
    if($i == 0){$p = 0;} else {  $w =$arr[$i - 1]; $p  += strlen($w) + 1;}
    if($arr[$i] == $finder){ if($position < 0){$position = $p;}$arr[$i] = $replacement;}}
    $newstring = implode(' ', $arr);
    echo $newstring; // gives: This is a monster 'cookie'
    echo '<br/>';
    echo $position;  // gives 10, the position of replaced element.
    

    For the position, the assumption is that the sentence has only single spaces because spaces are used in the explode and implode functions. Otherwise a case of double or larger spaces would require modifications, possibly by replacing spaces with a unique character or set of characters such as @$# which would be used as the first argument of the explode and implode functions.

    The code could be modified to capture more than one replacement, e.g. by capturing each replaced position in an array instead of testing for if(position < 0). This would also require to change the way $position is computed because its values are affected by the lengths of previous replacements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧