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;
Related
I have current module where I need to list all zip files from S3 AWS to the html tables. now I want to sub-string the 2 digit and the last digit of the number. however when i try to var dump the result still same number the substr not working can you help me guys to find out how to solved this?.
Example.
Number: 1150
Result must be: 11 and 50
I will show you guys my sample code that I already created.
$storage = Storage::disk('s3');
$client = $storage->getAdapter()->getClient();
$command = $client->getCommand('ListObjects');
$command['Bucket'] = $storage->getAdapter()->getBucket();
$command['Prefix'] = '11-10-2019';
$result = $client->execute($command);
foreach ($result['Contents'] as $file) {
$base_name = basename($file['Key']);
$trim_1 = str_replace('exp', '', $base_name);
$trim_2 = substr($trim_1, 1, -4);
var_dump($trim_2);
}
My Output is this:
$number = 1150;
$trim1 = substr($number, 0, 2);
$trim2 = substr($number,-2);
echo $trim1;
echo $trim2;
Based on what you ask you can achieve this by doing something like that.
The output is 11 & 50
Since you are using laravel you can use laravel helper for that which is pretty much the same and go for something like:
Str::substr($number, 2);
Str::substr($number, -2);
Don't forget to include the helper at the top of your file:
use Illuminate\Support\Str;
I'm working with indexing some news sites. A kind of news clipping.
I'm an amateur and curious. I'm not a programmer so the question may seem silly to anyone in the business. But if anyone can help, thank you.
The paging of the sites I was doing parsing was practically the same and I used this scheme:
$url = $ url. '/page/'. $s;
$next_url = $s + 1;
$prev_url = $s - 1;
if ($prev_url <= 0) {
$prev_url = 1;
}
The format was basically this:
http://example.com/politics/page/2
But yesterday I came across something different and I do not know how to page. I get this link format through preg_match_all:
http://www.example.com/browse-Politics-National-texts-1-date.html
This is the paging part:
-1-
This part is variable:
Political-National-texts
Any guidance?
If what you are asking for is parsing the url for the pagination and variable parts, you can use preg_match with the following regexp:
if (preg_match('/^http:\/\/www.example.com\/browse-([-a-zA-Z]+)-(\d+)-date\.html$/', $url, $matches)) {
var_export($matches);
}
Then you will get the result:
array (
0 => 'http://www.example.com/browse-Politics-National-texts-1-date.html',
1 => 'Politics-National-texts',
2 => '1',
)
The keys in $matches will be:
0: The entire match
1: The first matched group (the variable)
2: The second matched group (the pagination)
<?php
$url = 'http://www.example.com/browse-Politics-National-texts-1-date.html'
$url_basename = basename($url); // extract `browse-Politics-National-texts-1-date.html`
$url_exploded = explode('-',$url_basename); // make an array delimited by `-`
array_pop($url_exploded);
$url_page_number = array_pop($url_exploded); // get the 2nd element from back
?>
Result:
$url_page_number = 1
PS. Could make it shorter, but it's for educational purposes :-)
I have the following string after it has been parsed by regex:
FOO_BAR_FOOBA_*_R:FOO_*
I am supposed to get rid of "FOOBA" so I thought that just by doing, should have been enough.
$size[0] = array('qwe', 'rty', 'uiop', 'asdf');
$size[1] = array('ASD' , 'FGHJ' , 'ZXCVB');
$size[2] = array('lol' , 'cat' , 'woof');
$pieces = explode("_",$noGen);
foreach ($size[0] as $value){
$search = array_search($size[0], $pieces);
unset($pieces[$search]);
}
However, I know that the code above pretty much would have worked out until I found out that not all the time the third "piece" exists and that the second piece sometimes can contain same values as the third one.
Is there a way to do this without having to use a foreach per size. If it is not clear I could add more information or just ping me.
Thank you in advance!
I want to get specific content of a website into an array.
I have approx 20 sites to fetch the content and output in other ways i like.Only the port is always changing (not 27015, its than 27016 or so...)
This is just one: SOURCE-URL of Content
For now, i use this code in PHP to fetch the Gameicon "cs.png", but the icon varies in length - so it isn't the best way, or? :-/
$srvip = '148.251.78.214';
$srvlist = array('27015');
foreach ($srvlist as $srvport) {
$source = file_get_contents('http://www.gametracker.com/server_info/'.$srvip.':'.$srvport.'/');
$content = array(
"icon" => substr($source, strpos($source, 'game_icons64')+13, 6),
);
echo $content[icon];
}
Thanks for helping, some days are passed from my last PHP work :P
You just need to look for the first " that comes after the game_icons64 and read up to there.
$srvip = '148.251.78.214';
$srvlist = array('27015');
foreach ($srvlist as $srvport) {
$source = file_get_contents('http://www.gametracker.com/server_info/'.$srvip.':'.$srvport.'/');
// find the position right after game_icons64/
$first_occurance = strpos($source, 'game_icons64')+13;
// find the first occurance of " after game_icons64, where the src ends for the img
$second_occurance = strpos($source, '"', $first_occurance);
$content = array(
// take a substring starting at the end of game_icons64/ and ending just before the src attribute ends
"icon" => substr($source, $first_occurance, $second_occurance-$first_occurance),
);
echo $content['icon'];
}
Also, you had an error because you used [icon] and not ['icon']
Edit to match the second request involving multiple strings
$srvip = '148.251.78.214';
$srvlist = array('27015');
$content_strings = array( );
// the first 2 items are the string you are looking for in your first occurrence and how many chars to skip from that position
// the third is what should be the first char after the string you are looking for, so the first char that will not be copied
// the last item is how you want your array / program to register the string you are reading
$content_strings[] = array('game_icons64', 13, '"', 'icon');
// to add more items to your search, just copy paste the line above and change whatever you need from it
foreach ($srvlist as $srvport) {
$source = file_get_contents('http://www.gametracker.com/server_info/'.$srvip.':'.$srvport.'/');
$content = array();
foreach($content_strings as $k=>$v)
{
$first_occurance = strpos($source, $v[0])+$v[1];
$second_occurance = strpos($source, $v[2], $first_occurance);
$content[$v[3]] = substr($source, $first_occurance, $second_occurance-$first_occurance);
}
print_r($content);
}
Is there a way that I can search a variable starting from a given position and find the start position of a string that is in the variable backwards from the given start position.
So for example if I initially do $getstart = strpos($contents, 'position', 0);
I then want to do $getprevpos = prevstrpos($contents, 'previous token', $getstart);
Obviously there is no such function as prevstrpos but I hope you get what I mean.
Example text area (terrible example I now):
Here is an example where I want to find the previous token once I have found the start position of a text string.
you can strrpos( substr($contents, 0, $getstart), 'previous token')
Is there something wrong with strrpos()? If 'offset' is negative: "Negative values will stop searching at the specified point prior to the end of the string."
you can try this. I think it should would for all cases but you should probly test it a bit. Might be a bug here and there but you get the idea. Reverse everything and do a strpos on the reversed string
prevstrpos( $contents, $token, $start )
{
$revToken = strrev($token);
$revStart = strlen($token) - $start;
$revContent = strrev($content);
$revFoundPos = strpos( $revContent, $revToken, $revStart );
if( $revFoundPos != -1 )
{
$foundPos = strlen($token) - $revFoundPos;
}
else
{
$foundPos = -1;
}
return $foundPos;
}