I need to replace the special characters å,ä,ö (with a or o) in my WP database for all image attachments. How can I apply this search and replace only to affect these links?

Edit: Solved

  • Where exactly in attachments? – Rarst Jan 9 '14 at 16:03
  • I mean all the image links in the database that are attached to posts (under the wp_postmeta table). – Staffan Estberg Jan 9 '14 at 16:08
  • 1
    This is a PHP/SQL question, not a WordPress-specific question. Per the WPSE FAQ, questions are not inherently on-topic merely because they take place in the context of WordPress. – Chip Bennett Jan 9 '14 at 16:42
  • Also: post your solution as an answer, rather than as an edit to your question. – Chip Bennett Jan 9 '14 at 16:44
  • Ok! Sorry about that. – Staffan Estberg Jan 9 '14 at 16:47

Just do this with your string:

$yourString = preg_replace ( '/å/' , 'a' , $yourString )
$yourString = preg_replace ( '/ä/' , 'a' , $yourString )
$yourString = preg_replace ( '/ö/' , 'o' , $yourString )

If you just want to do it with an SQL sentence, then you can try this :

REPLACE(REPLACE(REPLACE(StringColumn, 'å', 'a') , 'ä', 'a') , 'ö', 'o') 

And you can use it in a SELECT, UPDATE, ... sentences, like this:


SET StringColumn = REPLACE(REPLACE(REPLACE(StringColumn, 'å', 'a') , 'ä', 'a') , 'ö', 'o') 
| improve this answer | |
  • Sorry if I was unclear, I'm looking for an SQL sequence that I can run to update everything in the database. – Staffan Estberg Jan 9 '14 at 16:09
  • All right. Check it now. – Manolo Jan 9 '14 at 16:25
  • I just tried a similar method. The problem is, under the wp_postmeta table you have all other sorts of content that should not have their characters replaced (such as custom field text content), I only want to target image links. You know a way of doing that? Edit: or target any files for that matter. – Staffan Estberg Jan 9 '14 at 16:28
  • Solved it! See updated post. – Staffan Estberg Jan 9 '14 at 16:38

Solved -

UPDATE wp_postmeta SET meta_value = REPLACE(REPLACE(REPLACE(meta_value, 'å', 'a') , 'ä', 'a') , 'ö', 'o')
WHERE LOWER(RIGHT(meta_value, 5)) = '.jpeg'
   OR LOWER(RIGHT(meta_value, 4)) IN ('.jpg', '.gif', '.png')
| improve this answer | |
  • Great solution! – RCNeil Jul 22 '17 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.