# Images in body

**URL:** <https://www.ruby-forum.com/t/images-in-body/68087>\
**Category:** Rails\
**Created:** [September 9, 2006, 1:33am UTC](https://www.ruby-forum.com/t/images-in-body/68087 "2006-09-09T01:33:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fr.ivoloSus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fr.ivoloSus](https://www.ruby-forum.com/u/fr.ivoloSus)\
**Post date:** [September 9, 2006, 1:33am UTC](https://www.ruby-forum.com/t/images-in-body/68087/1 "2006-09-09T01:33:22Z")

</div>

Hi All,

I need a way to place floated images into the body of an article. The  
body is pulled from the database, so I need a way to scan a certain  
number of words, or paragraphs, and then place an image\_tag in there.

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![fr.ivoloSus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fr.ivoloSus](https://www.ruby-forum.com/u/fr.ivoloSus)\
**Post date:** [September 9, 2006, 3:21am UTC](https://www.ruby-forum.com/t/images-in-body/68087/2 "2006-09-09T03:21:31Z")

</div>

I’d recommend you look to another list (css-d for example) for help like  
this.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![fr.ivoloSus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fr.ivoloSus](https://www.ruby-forum.com/u/fr.ivoloSus)\
**Post date:** [September 9, 2006, 5:45am UTC](https://www.ruby-forum.com/t/images-in-body/68087/3 "2006-09-09T05:45:45Z")

</div>

This isn’t a css question. What I need to know is how to split up a  
large amount of text into paragraphs, based on newlines (as entered in  
a textarea) or the occurrence of a closing paragraph tag and an open  
one, and insert imagetags in between them. Ideally, what I need is  
the equivalent of php’s substr\_replace function.

This is probably a rudimentary question, but it’s definitely not a css  
question.

---

<div class="post-metadata">

**Author:** ![fr.ivoloSus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fr.ivoloSus](https://www.ruby-forum.com/u/fr.ivoloSus)\
**Post date:** [September 9, 2006, 11:40am UTC](https://www.ruby-forum.com/t/images-in-body/68087/4 "2006-09-09T11:40:05Z")

</div>

That’s great (both your solution and suggestion).

Thanks

---

<div class="post-metadata">

**Author:** ![fr.ivoloSus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fr.ivoloSus](https://www.ruby-forum.com/u/fr.ivoloSus)\
**Post date:** [September 9, 2006, 11:22am UTC](https://www.ruby-forum.com/t/images-in-body/68087/5 "2006-09-09T11:22:04Z")

</div>

> This isn’t a css question. What I need to know is how to split up a  
> large amount of text into paragraphs, based on newlines (as entered in  
> a textarea) or the occurrence of a closing paragraph tag and an open  
> one, and insert imagetags in between them. Ideally, what I need is  
> the equivalent of php’s substr\_replace function.

The equivalent function you’re after is ruby’s String gsub function  
which will replace stuff based on a regular expression:

content = content.gsub(/\</p\>\<p\>/, “

” \<\< image\_tag(“myimage.jpg”)  
\<\< “

”)

This is obviously a _very_ contrived and buggy example, but it should  
demonstrate the concept. You can also modify a string in place using  
string.gsub!()

If you haven’t already, I’d recommend you get the pragmatic programmers’  
‘Programming Ruby’ book (otherwise known as the ‘pickaxe’). It’ll  
really take you further into ruby itself and help you easily solve  
problems like this.

Hope that helps,

Steve
