Skip to content

Instantly share code, notes, and snippets.

@jon-kim

jon-kim/data.csv

Created Sep 3, 2019
Embed
What would you like to do?
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
<?php
$results = [];
$strings = [
'Some random string that also has this string: [soundcloud url="https://api.soundcloud.com/tracks/910291" params="auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;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&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;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&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true" width="100%" height="450" iframe="true" /] in i112222t.\']'
];
$strMatch = [
preg_quote('[soundcloud url="', '/'),
preg_quote('" params="auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;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