dsc56927 2018-10-11 20:05
浏览 124
已采纳

PHP - 用星号替换字符,除非有减号

How can I replace a string with stars except the first and the last letter but not a minus in case there is one. Here for better illustration what I try to get: From: url-name To u**-***e

This is what I have so far:

function get_starred($str) {
        $len = strlen($str);
return substr($str, 0, 1).str_repeat('_', $len - 2).substr($str, $len - 1, 1);
}
  • 写回答

4条回答 默认 最新

  • douluan8828 2018-10-11 20:12
    关注

    hey try implmenting the following:

    function get_starred($str) {
      $str_array =str_split($str);
     foreach($str_array as $key => $char) {
      if($key == 0 || $key == count($str_array)-1) continue;
      if($char != '-') $str[$key] = '*';
     }
      return $str;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题