Jeyalakshmi Posted July 6, 2006 Share Posted July 6, 2006 <?php$string = "This is a test";echo str_replace(" is", " was", $string);echo ereg_replace("( )is", "\\1was", $string);echo ereg_replace("(( )is)", "\\2was", $string);?> output is :This was a testThis was a testThis was a testcan any one know how this has come Link to comment Share on other sites More sharing options...
ThePsion5 Posted July 6, 2006 Share Posted July 6, 2006 It seems like you're trying to perform a simple string replace function with this code, why not use PHP's string replace function? If that's not the case, you'll have to more clearly tell us what's happening and what you want to happen. Link to comment Share on other sites More sharing options...
pulpfiction Posted July 6, 2006 Share Posted July 6, 2006 Example used by Jeyalakshmi is from the php.net tuts, http://us2.php.net/ereg_replaceand propably needs explanation how ereg_replace with parenthesis works... Link to comment Share on other sites More sharing options...
justsomeguy Posted July 6, 2006 Share Posted July 6, 2006 <?php$string = "This is a test";echo str_replace(" is", " was", $string);echo ereg_replace("( )is", "\\1was", $string);echo ereg_replace("(( )is)", "\\2was", $string);?>You're telling it to do the same thing 3 times, which is always to replace " is" with " was". The first replace is straightforward. The second replace uses parentheses to specify that the space is a subgroup (which isn't necessary for this example), and the replacement is the word "was" preceded by the same pattern that "is" matched on. Since it can only match on a space, the replacement will be " was". The third replacement is doing the same thing, but it also creates a second subgroup out of the entire thing, and once again the replacement matces with " was".What do you expect to happen? Link to comment Share on other sites More sharing options...
Jeyalakshmi Posted July 7, 2006 Author Share Posted July 7, 2006 You're telling it to do the same thing 3 times, which is always to replace " is" with " was". The first replace is straightforward. The second replace uses parentheses to specify that the space is a subgroup (which isn't necessary for this example), and the replacement is the word "was" preceded by the same pattern that "is" matched on. Since it can only match on a space, the replacement will be " was". The third replacement is doing the same thing, but it also creates a second subgroup out of the entire thing, and once again the replacement matces with " was".What do you expect to happen?<{POST_SNAPBACK}> i cant understand that sub group and all could you please explain me more clearly Link to comment Share on other sites More sharing options...
justsomeguy Posted July 7, 2006 Share Posted July 7, 2006 A subpattern is anything inside parentheses. It is used to break up the regular expression into pieces, and you can refer to each piece later on. The "\1was" refers to the first subpattern, which is a space, followed by "was". The "\2was" refers to the second subpattern, which is a space, followed by "was". The reason the space is the second subpattern instead of the first subpattern in the third example is because the first subpattern in that example is the entire pattern, which is also surrounded by parentheses.If you are trying to learn about regular expressions, you will want to read this:http://www.php.net/manual/en/reference.pcr...tern.syntax.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now