replacing text with uper and lower case

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

replacing text with uper and lower case

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: replacing text with uper and lower case

Post 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.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: replacing text with uper and lower case

Post 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
Post Reply