Skip to content

Instantly share code, notes, and snippets.

@georgiana-gligor
Last active December 20, 2018 07:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgiana-gligor/37c84bc0a21d3bbea7957d08d03317a2 to your computer and use it in GitHub Desktop.
Save georgiana-gligor/37c84bc0a21d3bbea7957d08d03317a2 to your computer and use it in GitHub Desktop.
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