1

Hey folks, I have a field within a table that I need to do a search and replace on, though it's a bit tricky and beyond my measure of querying skills. Basically, the contents of a field may look like this:

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg

I need to do a search and replace that would replace each line with something like:

<img src="http://www.domain.com/image1.jpg" />
<img src="http://www.domain.com/image2.jpg" />
<img src="http://www.domain.com/image3.jpg" />
<img src="http://www.domain.com/image4.jpg" />
<img src="http://www.domain.com/image5.jpg" />

The image titles are completely random and could be .gif, .jpg or pngs. The table name is "exp_channel_data" and the field name is "field_id_8" and I'd be running the query in phpadmin. Is the above search and replace possible?

2
  • what possible reason not to do it client side? Commented Aug 2, 2010 at 15:01
  • The database is part of a CMS. After some version updates, etc etc, running a query through phpadmin is definitely my best and only option. Commented Aug 2, 2010 at 15:03

3 Answers 3

4

I believe you can replace line breaks with a closing + opening tag and then wrap the result in an img tag to get the desired result:

UPDATE exp_channel_data
SET field = CONCAT(
    '<img src="http://www.domain.com/',
    REPLACE(field_id_8, '\n',  '" />\n<img src="http://www.domain.com/'),
    '" />')
WHERE (field_id_8 IS NOT NULL) AND (field_id_8 != '')
Sign up to request clarification or add additional context in comments.

3 Comments

+1, I was just about to expand my answer to basically the same thing.
This worked beautifully good sir! Thank you so much. Much more efficient then the query i was trying to string together :)
Actually, how would this be modified to skip over any of the rows where that field is NULL or empty?
0

Yes, it is. I hope your Regular Expression skills are up-to-date.

1 Comment

Thanks. Unfortunately my Regular Expression skills are not up to date. I just can't seem to wrap my head around how to do this. Can you show me a quick dirty example of how to do something like this?
0

Here's a link to REPLACE()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.