Page 1 of 1

replacing text with uper and lower case

Posted: Tue Jul 29, 2008 10:42 am
by someguyhere
Is there a function in php to replace text with a variable but have it mainting the case of the text being replaced? For example,

$input1 = some word
Then I want to go out and find all occurances of "some other word" and "Some Other Word" and replace them with input1, but maintain their original case so they would become "some word" and "Some Word". Hopefully, my explaination makes sense. Is there an easy way to do this?

Thanks in advance for any help. I just started teaching myself php on the side about 2 weeks ago, so I don't even know what to look for.

Re: replacing text with uper and lower case

Posted: Tue Jul 29, 2008 10:59 am
by onion2k
I don't think there's an 'easy' way to do it. There certainly isn't a function to do it. I imagine your options are:

1. generate all the possible variations and then loop through them all.. bit of a hassle though, and awfully slow.
2. find all the positions of the needle words, then replace them more intelligently than str_replace can manage.
3. possibly use a regular expression. Not sure if that's possible though.

Re: replacing text with uper and lower case

Posted: Tue Jul 29, 2008 11:17 am
by WebbieDave
If you needed to replace "History of the World" with "past events", which words would be capitalized, if any?

Either way, you could try one of the regex functions that have a callback.
http://us.php.net/manual/en/function.pr ... llback.php