简体   繁体   中英

PHP how can i hide part of url

I'm working on a project and need to hide part of a url on the output result of my php file, how can i do that?

the piece of code

    if (!$foundPlaylist){
                $playList=array(                
['publishedAt'], 
                            'thumbId'     => $entry[$i]['snippet']['thumbnails']['medium']['url'],
                            'videosCount' => $videoCount,
                            'videos'      => getVideos($entry[$i]['snippet']['resourceId']['videoId']) 
                        );
                array_push($MainFeed,$playList);    
            }

The result

{ "feed":[{"thumbId":"https://i.ytimg.com/vi/SEchOz24pd8/mqdefault.jpg","videosCount":20,"videoid":"SEchOz24pd8",}],"0":

I need to hide https://i.ytimg.com/vi/ and /mqdefault.jpg from thumbId.

Just use

substr($entry[$i]['snippet']['thumbnails']['medium']['url'], 23, 11);

to select only the part of the URL between position 23 and (23+11) = 34

This, of course, only works if you know the string length is going to be exactly the same for all users. If you know the string length will differ, Anthony's answer might help you out.

I find this most readable:

$path = parse_url(
    $entry[$i]['snippet']['thumbnails']['medium']['url'],
    PHP_URL_PATH
);
list($user, $code, $image) = explode('/', $path);

echo $code;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM