开发者

Find and replace in a block of text

I have a database full of text fields that look like this:

开发者_StackOverflow社区
(paragraph of normal text)

image:blog/clownboy.jpg

(another paragraph)

I'm trying to write a view helper for Rails that will take one of these big blocks of text, find bits like "image:blog/clownboy.jpg" and replace them with the corresponding <img src="blog/clownboy.jpg"> (without disturbing the surrounding whitespace) before outputting it to the user. I've been trying for an hour or so now, but I'm new to Ruby and the regular expressions are still a bit beyond me.


Global substitution of image: xyz on a separate line with <img src='xyz'/>:

text.gsub!(/^image:(.+)$/) { "<img src='#{$1}'/>" }


What database are you using? You might want to do this replacement on the query level ...

The REPLACE function would work, you can use that in MySQL, SQL Server, etc.

SELECT REPLACE('abcdefghicde','cde','xxx');

http://www.electrictoolbox.com/mysql-select-replace/

http://msdn.microsoft.com/en-us/library/ms186862.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜