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

检查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!

图片转代码服务由CSDN问答提供 功能建议

我是一名应用程序的iOS开发人员,这是一个很长的故事,但我们的自由API开发人员“不可用”。 因此,我是laravel / PHP的新手,并尝试修复我们的API上的错误。

基本上,当用户默认注册时,我们会将他们的Facebook个人资料图片保存下来 数据库中的URL作为应用程序的个人资料图片。 当他们添加自定义图片时,会使用随机字母数字字符串保存进行保存,并在 profile_pic 列中设置该字符串。 当 JSON 对象中返回 profile_pic 时,我们的基本URL被附加到开头。

所以问题是这个基础 URL也会附加到Facebook图像URL的开头,以便它看起来像 https://base.url/https://facebook.url ,这意味着用户将无法看到图像 ,只是默认的占位符灰色。 我希望能够检查URL是否已经以某个值开始。

这是我到目前为止所做的:

  public function getProfilePicAttribute($ value){
 $ fbUrl =“https://scontent.xx.fbcdn.net”
 $ length = strlen($ fbUrl); 
 if(substr($ value,0,$) 长度)=== $ fbUrl){
返回$ value 
} 
返回'https://'。  env('VDT_DOMAIN')。  '/ uploads / profile_pic /'。  $ value; 
} 
   
 
 

这不起作用所以我想知道是否有人能够帮助我。 提前感谢任何建议!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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');
    
    已采纳该答案
    打赏 评论
  • dongyaofu0599 2017-04-20 17:05

    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;
    
    打赏 评论
  • donglin6109 2017-04-20 20:17

    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

    打赏 评论

相关推荐 更多相似问题