I have just migrated someone's site to Drupal along with all images but I need to change the img src path and not the file name

basically

<img src="b.image.com/kittykat.png" />

needs to become 

<img src="/images/kittykat.png" /> 

I am mostly having trouble with the preg replace pattern

Comments

nevets’s picture

What does your code look like?

mrwendell’s picture

I have done this type of transition before by doing a database dump then a grep find/replace to catch all the possible iterations of the URL,

eg

<img src="b.image.com/kittykat.png" />
<img src="http://b.image.com/kittykat.png" />
<img src="http://www.b.image.com/kittykat.png" />

This is a pretty drastic procedure but can work if you good with a text editor. In my case I did a bulk 30,000+ with a complex grep find/replace command and everything actually worked. Make sure you have everything backed up. Its also best to do on a development server first!!! You will like have to take a couple runs at it to work out all the possible problems

GOOD LUCK!

nevets’s picture

At the database level you can also use the replace statement, something like

UPDATE table_name SET field_name = REPLACE(field_name, 'b.image.com', '/images');

replacing table_name and field_name (twice) with the appropriate names.

This will generally be faster than dumping to a text file, changing and re-importing.

mrwendell’s picture

You also need to be careful for other img src references. For example in my case we also had images in blocks and full URL's specified for theme settings (e.g. logos), not to mention images even in the header area of custom views. Basically all over the place. Dumping the Database was the easiest way to hit everything at once.

That said, nevets suggestion may work well if you are certain that you have a simple find/replace to do ONLY in your node bodies.

all the best

netentropy’s picture

I have been trying to use form api and php to load each node body, search for all img tags, find and replace the source url before the last marker

can you use wildcards in the sql statement

the crappy thing is the last folder name changes before the file name.

nevets’s picture

Can you provide some actual examples of what "last folder name changes before the file name" and what they need to map to?

netentropy’s picture

@nevets

Thanks with your help on this.


http://2.bp.blogspot.com/-WnH7Cc1qNII/TV6dVQvsv4I/AAAAAAAAArs/tLAOCG2P9hw/s1600/napster

http://4.bp.blogspot.com/-8sUadwpZ-5s/TV6dM1NEX_I/AAAAAAAAArc/F2Hk8SGADqw/s320/Billy%2BJ%2B1

basically I need everything before the last marker (/) to become /images/

http://2.bp.blogspot.com/-WnH7Cc1qNII/TV6dVQvsv4I/AAAAAAAAArs/tLAOCG2P9hw/s1600/napster

becomes

images/napster