Welcome Guest, Not a member yet? Register   Sign In
replace string in query result
#1

[eluser]aryan_[/eluser]
I want to replace a specific string in query result, like:

Lorem ipsum dolor email@[domain] sit amer info@[domain].

Here I want to replace [domain] with a specific string (may be some domain name). The db field is text type and this replacement could happen at 10-20 place in the text. Text size could be more than 1000 words.

What's the CI way (or any best way) to do this. I can use php string replace but looking for a faster and more efficient way.

Thanks!
#2

[eluser]PravinS[/eluser]
use MySQL replace function

http://dev.mysql.com/doc/refman/5.1/en/s...on_replace
#3

[eluser]aryan_[/eluser]
[quote author="PravinS" date="1375442977"]use MySQL replace function

http://dev.mysql.com/doc/refman/5.1/en/s...on_replace[/quote]

Thanks! I was thinking about it. Is it the best way?
#4

[eluser]boltsabre[/eluser]
It's a bit of a catch 22 this problem.

You've got to ask yourself, is it a bottleneck and is the tiny amount of time this is likey to save worth all the consideration.
Both functions have been optimised to run as fast as they can (but research using strstr instead of str_replace in php).

Another thing to consider is code maintenance, what is going to be more maintainable in the future?

And using the MYSQL replace is fine, but if you've got a really high load DB it will add extra time for the query to execute, which can lead to table locking issues (I think).

And another thing... it doesn't really make sense to be running the php str_replace on output every time the data comes out of the DB if it is possible to do it when the data is inserted (not always possible but where it is, you should always do your string manipulation once before/during insert, not every time on output).

All in all, it all just really depends on what your trying to accomplish, and unless it's already a bottleneck you are unlikely to see any difference what-so-ever whatever approach you choose.

But... why don't you test it and see for yourself!!!




Theme © iAndrew 2016 - Forum software by © MyBB