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_pic
is 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
Instead of truncating the string to compare, you can use
strpos
(http://php.net/manual/en/function.strpos.php).Laravel has great
starts_with()
helper. For example, this will returntrue
:this is to show the usage of the method indicated above by Alexey
where the base.url is whatever url is getting concatenated before the value
You can use Str::startsWith() helper…