Hey Peter, thanks for the code!! The regex works nicely… have added it to the upgrade script for 5.1.4 (hopefully released next weekend)…
I had to tweak a few other things to use our api and get it working… for the reference, here is our function:
function sp_fix_shortened_links() {
$postCount = spdb_count(SFPOSTS);
$limit = 1000;
for ($offset = 0; $offset < $postCount; $offset+= $limit) {
$posts = spdb_select('set', "SELECT post_id, post_content FROM ".SFPOSTS." ORDER BY post_id ASC LIMIT $offset, $limit");
foreach ($posts as $post) {
/*
Matches would be:
0 = The entire string
1 = The link in the <a> tag
2 = The rest of the parameters in the <a> tag (nofollow, target, etc.)
3 = The part after the 5 periods
So then we want to replace 0 with 1 for each result
We are assuming that the links have 5 consecutive periods
*/
$postContent = stripslashes($post->post_content);
preg_match_all("/<a href="(.*?)"(.*?).....(.*?)</a>/is", $postContent, $linkMatches);
if (!empty($linkMatches[0])) {
foreach ($linkMatches[0] as $index => $stringMatch) {
$postContent = str_replace($stringMatch, $linkMatches[1][$index], $postContent);
}
$postContent = esc_sql($postContent);
spdb_query("UPDATE ".SFPOSTS." SET post_content = '$postContent' WHERE post_id = $post->post_id");
}
}
}
}
might be worth a quick check to see I didnt screw anything up! ![]()
1000 seemed to work faster than 100 on a larger db… had to move the update query outside the foreach for when multiple shortened links in the content…
again, THANKS!