Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
preg_replace vs str_replace
<?php
$channels = array(
"UCgpy7yxv_7JbR26McdS1uQA",
"UCrHZJ6fddxeK2wwPIh5-O4Q",
"UCYUI-AaHyYslLLWAss4EiAA",
);
$res = 5;
$key = 'YOUR_KEY_HERE';
$results = new ArrayObject();
$criteria = sprintf(
'&maxResults=%s&order=date&key=%s&fields=items(id)',
$res, $key
);
$baseUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet';
foreach ($channels as $user) {
$endpoint = sprintf(
'%s&channelId=%s%s',
$baseUrl,
$user,
$criteria
);
$scraped = file_get_contents($endpoint);
$decoded = json_decode($scraped, true);
$results->append($decoded);
/*
* sfat: fa un sleep aici sa nu iei mereu imediat urmatorul element,
* e posibil sa te baneze la un moment dat
*/
}
foreach ($results as $id) {
foreach ($id['items'] as $item) {
foreach ($item as $clip) {
echo $clip['videoId'] . PHP_EOL;
}
}
}
<?php
$original = 'prepend/youtube#video/postpend';
$counter = 1000;
$start = microtime(true);
for ($walk = 0; $walk < $counter; $walk++) {
$computed = preg_replace('/youtube#video/', '', $original);
}
$end = microtime(true);
echo sprintf('preg_replace took %f', $end - $start) . PHP_EOL;
$start = microtime(true);
for ($walk = 0; $walk < $counter; $walk++) {
$computed = str_replace('/youtube#video/', '', $original);
}
$end = microtime(true);
echo sprintf('str_replace took %f', $end - $start) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.