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

检查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条)

报告相同问题?