dpn68721 2017-04-20 17:03
浏览 131
已采纳

检查String是否以字符串开头

I am lead iOS dev on an application and it's a long story but our freelance API dev is 'unavailable'. So I'm a complete newbie to laravel/PHP and trying to fix a bug on our API.

Basically, when a user signs up by default we take their Facebook profile picture and save it's URL in the database as their profile picture for the app. When they add a custom picture it is saved with a random alphanumeric string save and that string is set in the profile_pic column. When the profile_picis returned within the JSON object our base URL is appended to the start.

So the problem is that this base URL is also appended to the start of the Facebook image URL so that it would look like https://base.url/https://facebook.url which means a user won't see the image, just the default placeholder grey colour. I would like to be able to check whether the URL already starts with a certain value.

This is what I have so far:

public function getProfilePicAttribute($value){
     $fbUrl = "https://scontent.xx.fbcdn.net"
     $length = strlen($fbUrl);
     if (substr($value, 0, $length) === $fbUrl) {
         return $value
     }
     return 'https://' . env('VDT_DOMAIN') . '/uploads/profile_pic/' . $value;
}

This doesn't work so I was wondering if anybody would be able to help me out with it. Thanks in advance for any advice!

  • 写回答

3条回答 默认 最新

  • drug95107 2017-04-20 17:26
    关注

    Laravel has great starts_with() helper. For example, this will return true:

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示