开发者

Find and replace in a block of text

开发者 https://www.devze.com 2022-12-24 14:53 出处:网络
I have a database full of text fields that look like this: 开发者_StackOverflow社区 (paragraph of normal 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

精彩评论

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