1

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

5
  • Where exactly in attachments?
    – Rarst
    Commented Jan 9, 2014 at 16:03
  • I mean all the image links in the database that are attached to posts (under the wp_postmeta table). Commented Jan 9, 2014 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. Commented Jan 9, 2014 at 16:42
  • Also: post your solution as an answer, rather than as an edit to your question. Commented Jan 9, 2014 at 16:44
  • Ok! Sorry about that. Commented Jan 9, 2014 at 16:47

2 Answers 2

2

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')
1
  • Great solution!
    – RCNeil
    Commented Jul 22, 2017 at 3:50
1

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:

UPDATE MyTable

SET StringColumn = REPLACE(REPLACE(REPLACE(StringColumn, 'å', 'a') , 'ä', 'a') , 'ö', 'o') 
4
  • Sorry if I was unclear, I'm looking for an SQL sequence that I can run to update everything in the database. Commented Jan 9, 2014 at 16:09
  • All right. Check it now.
    – Manolo
    Commented Jan 9, 2014 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. Commented Jan 9, 2014 at 16:28
  • Solved it! See updated post. Commented Jan 9, 2014 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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