Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

str_replace for exact match

Question posted by: pankajit09 (Familiar Sight) on May 9th, 2008 01:47 PM
Hello,

Code: ( text )
  1. $x = "tyu"
  2.  
  3. $y="3"
  4.  
  5. $eq = "asa+tyu-tyut+1"
  6.  
  7. $result = str_replace($x,$y,$eq);
  8.  
  9. echo $result;



$result has "asa+3-3t+1"

I expect "asa+3-tyut+1"

Please help .
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
dlite922's Avatar
dlite922
Needs Regular Fix
491 Posts
May 10th, 2008
08:05 AM
#2

Re: str_replace for exact match
Quote:
Originally Posted by pankajit09
Hello,

Code: ( text )
  1. $x = "tyu"
  2.  
  3. $y="3"
  4.  
  5. $eq = "asa+tyu-tyut+1"
  6.  
  7. $result = str_replace($x,$y,$eq);
  8.  
  9. echo $result;



$result has "asa+3-3t+1"

I expect "asa+3-tyut+1"

Please help .


That's pretty difficult to do, if not impossible unless you find some other pattern in your string.

use preg_replace() http://us2.php.net/preg_replace

to implent some kind of pattern...

for example

does it always end with t, those that end with "t" do not replace?

does it have to end with "-" everytime?

should it end with any characters except a through z?

This will help you find a regex that can do what you want.

I'm no regex expert but I think if it comes down to that and you have all your constraints, we can build one, experiment and make it work together.



-Dan

Reply
pankajit09's Avatar
pankajit09
Familiar Sight
249 Posts
May 12th, 2008
09:01 AM
#3

Re: str_replace for exact match
Quote:
Originally Posted by dlite922
That's pretty difficult to do, if not impossible unless you find some other pattern in your string.

use preg_replace() http://us2.php.net/preg_replace

to implent some kind of pattern...

for example

does it always end with t, those that end with "t" do not replace?

does it have to end with "-" everytime?

should it end with any characters except a through z?

This will help you find a regex that can do what you want.

I'm no regex expert but I think if it comes down to that and you have all your constraints, we can build one, experiment and make it work together.



-Dan



Yes you are right.

I am using -->

Code: ( text )
  1. $eq = preg_replace("/\b".$param."\b/", $paramData, $eq);

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,100 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors