Created
September 3, 2019 06:00
-
-
Save jon-kim/13b428c651bd996cc622f6cc8de4e72e to your computer and use it in GitHub Desktop.
This file contains 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
soundcloud | different_url | |
---|---|---|
https://api.soundcloud.com/tracks/910291 | https://api.different-embed.com/12345 | |
https://api.soundcloud.com/tracks/5678 | https://api.different-embed.com/562222278 | |
https://api.soundcloud.com/tracks/12345 | https://api.different-embed.com/89999999 |
This file contains 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 | |
$results = []; | |
$strings = [ | |
'Some random string that also has this string: [soundcloud url="https://api.soundcloud.com/tracks/910291" params="auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true" width="100%" height="450" iframe="true" /] in it.', | |
'Another completely random string that also has this string: [soundcloud url="https://api.soundcloud.com/tracks/5678?secret=test" params="auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true" width="100%" height="450" iframe="true" /] in it test .', | |
'Something else completely random string that also has this string: [soundcloud url="https://api.soundcloud.com/tracks/12345?secret=test" params="auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true" width="100%" height="450" iframe="true" /] in i112222t.\']' | |
]; | |
$strMatch = [ | |
preg_quote('[soundcloud url="', '/'), | |
preg_quote('" params="auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true" width="100%" height="450" iframe="true" /]', '/') | |
]; | |
$csv = array_map('str_getcsv', file('data.csv')); | |
array_shift($csv); | |
foreach ($csv as $c) { | |
$arraySearch = $c[0]; | |
$searchResult = array_filter ($strings, function($item) use ($arraySearch) { | |
return (strpos($item, $arraySearch) !== false); | |
}); | |
foreach ($searchResult as $key => $value) { | |
$preg_result = preg_split("/{$strMatch[0]}|{$strMatch[1]}/", $value); | |
if (count($preg_result)==3 && isset($strings[$key])) { | |
$replaced = str_replace($c[0], $c[1], $preg_result[1]); | |
$results[$key] = "{$preg_result[0]}[different-embed src=\"$replaced\"]{$preg_result[2]}"; | |
unset($strings[$key]); | |
} | |
} | |
$results = $results + $strings; | |
} | |
ksort($results); | |
var_dump($results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment