[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

STR_REPLACE TROUBLE

Hello, I have a problem using str_replace and I'm wandering if it's not a bug of PHP.

I'm using PHP 5.3.5 compiled with MSVC6 (Visual C++ 6.0) , running with Apache 2.2.17 and windows 2008 server

I want to replace only the first occurence of a string:
$num_of_replace = 1;
str_replace("REF_PRODUIT"," COUNT(*) ","SELECT REF_PRODUIT FROM PRODUITS WHERE REF_PRODUIT = 'BIN24101' ",$num_of_replace)
the expected result is
SELECT COUNT(*) FROM PRODUITS WHERE REF_PRODUIT = 'BIN24101'  // GOOD
but it seem to not use the parameter $num_of_replace and it replace ALL occurences:
SELECT COUNT(*) FROM PRODUITS WHERE COUNT(*) = 'BIN24101'  // NOT GOOD

is there anything wrong in my code ??

Thanks for yur help !


0
bruno_boccara
Asked:
bruno_boccara
  • 5
  • 4
  • 3
2 Solutions
 
R-ByterCommented:
Use preg_replace for that:

preg_replace("/REF_PRODUIT/"," COUNT(*) ","SELECT REF_PRODUIT FROM PRODUITS WHERE REF_PRODUIT = 'BIN24101' ",$num_of_replace)

Open in new window


Regards
0
 
bruno_boccaraAuthor Commented:
I found, this is a bug ....

http://bugs.php.net/bug.php?id=11457


0
 
Beverley PortlockCommented:
The parameter you are using for $num_of_replace is how many replacements str_replace MADE not how many you would like it to MAKE.
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
bruno_boccaraAuthor Commented:
YOU'r right

I just see this right now...

I should use preg_replace instead.....
0
 
bruno_boccaraAuthor Commented:
SORRY SORRY
I DIDN'T SEE R-Byter ANSWER !!!

How can I fix my mistake ???
0
 
R-ByterCommented:
You can report it to mods.
But its not so important. Im glad that you have working solution. Those are my points.

Regards
0
 
Beverley PortlockCommented:
I've no objection if you want to re-open the question and assign the points differently. I'm not exactly short of points.......

0
 
bruno_boccaraAuthor Commented:
at least I will give 250 points to each of you for your help !

I ask for the moderator ....
0
 
R-ByterCommented:
@bportlock:

Just to be clear that I wasn't complaining. :)
0
 
Beverley PortlockCommented:
@R-Byter - It's OK. This is the correct thing to do. I was surprised to have my contribution accepted, I expected perhaps an assist, no more.
0
 
bruno_boccaraAuthor Commented:
Thanks to R-Byter and to bportlock for your help !!!
0
 
R-ByterCommented:
You're welcome.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now