skip to Main Content

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!

4

Answers


  1. Instead of truncating the string to compare, you can use strpos (http://php.net/manual/en/function.strpos.php).

    if (strpos($value, $fbURL) === 0) {
        return $value;
    }
    return 'https://' . env('VDT_DOMAIN') . '/uploads/profile_pic/' . $value;
    
    Login or Signup to reply.
  2. Laravel has great starts_with() helper. For example, this will return true:

    starts_with('This is my name', 'This');
    
    Login or Signup to reply.
  3. this is to show the usage of the method indicated above by Alexey

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

    where the base.url is whatever url is getting concatenated before the value

    Login or Signup to reply.
  4. You can use Str::startsWith() helper…

    IlluminateSupportStr::startsWith('This is my string', 'This');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search