views:

109

answers:

3

I have strings in my application that users can send via a form, and they can optionally replace words in that string with replacements that they also specify. For example if one of my users entered this string:

I am a user string and I need to be parsed.

And chose to replace and with foo the resulting string should be:

I am a user string foo I need to be parsed.

I need to somehow find the starting position of what they want to replace, replace it with the word they want and then tie it all together.

Could anyone write this up or at least provide an algorithm? My PHP skills aren't really up to the task :(

Thanks. :)

+2  A: 

use preg_replace. You don't need to think so hard about this though you will have to learn a little bit about regexes. :)

Chuck Vose
Regexes are TOTALLY worth it though. This is the answer you need. Take the time up front to read a little about them and how they work. You'll be able to use it for a lot of surprisingly cool things!
Crowe T. Robot
Haha yeah they are. I think regexes changed my life. That might be a little weird...
Chuck Vose
It would be an idea to have a callback inside the regex to work out what word to replace it with. The complex bit is creating a regex that matches words assuming.people,dont.talk.-entering sentences+like this. I mean, you still understood that, right? ;-)
Gary Green
For the example given however, I think regular expressions is a bit overkill. A simple str_replace would do the job.
Mikael S
That's probably true, but it would be a good idea to learn both. preg_replace will allow you to do much more complex things and when you decide that str_replace no longer fits what you needed it to do initially you'll know preg_replace and you'll start to get a feeling for where they both apply.
Chuck Vose
+2  A: 

Read up on str_replace, or for more complex replacements on Regular Expressions and preg_replace.

Examples for both:

<?php
$str = 'I am a user string and I need to be parsed.';
echo str_replace( 'and', 'foo', $str ) . "\n";
echo preg_replace( '/and/', 'foo', $str ) . "\n";
?>


In response to the comments of this answer, note that both examples above will replace every occurrence of the search string (and), even when it happens to be within another word.

To take care of that you either have to add the word separators to the str_replace call (see the comment of an example), but this will get quite complicated when you want to take care of all common word separators (space, commas, dots, exclamation marks, question marks etc.).

An easier to way to fix this problem is to use the power of regular expressions and make sure, the actual search string is not found within another word. See Tim Pietzcker's example below for a possible solution.

poke
"I am the sandman." --> "I am the sfooman."
Tim Pietzcker
@Tim Pietzcker: `echo str_replace( ' and ', ' foo ', $str ) . "\n";`
Brendan Long
@Brendan: "But what if he tries that (and it still doesn't work)?" :)
Tim Pietzcker
Well, fair enough; if you look at it and want to replace word-wise ;) I'll add something to my answer to take care of that :)
poke
+2  A: 
$result = preg_replace('/\band\b/i', 'foo', $subject);

will find all occurences of and where it's a word on its own and replace it with foo. \b ensures that there is a word boundary before and after and.

Tim Pietzcker
If the user is choosing the words to find/replace, you should use `preg_quote($find, "/")` to make sure they don't break the regular expression.
mcrumley