Last active
December 20, 2018 07:40
-
-
Save georgiana-gligor/37c84bc0a21d3bbea7957d08d03317a2 to your computer and use it in GitHub Desktop.
preg_replace vs str_replace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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