Solved

STR_REPLACE TROUBLE

Posted on 2011-02-28
13
431 Views
Last Modified: 2012-05-11
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
Comment
Question by:bruno_boccara
  • 5
  • 4
  • 3
13 Comments
 
LVL 14

Accepted Solution

by:
R-Byter earned 250 total points
ID: 34999338
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
 

Author Comment

by:bruno_boccara
ID: 34999482
I found, this is a bug ....

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


0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 34999532
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bruno_boccara
ID: 34999664
YOU'r right

I just see this right now...

I should use preg_replace instead.....
0
 

Author Comment

by:bruno_boccara
ID: 34999686
SORRY SORRY
I DIDN'T SEE R-Byter ANSWER !!!

How can I fix my mistake ???
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 35000086
You can report it to mods.
But its not so important. Im glad that you have working solution. Those are my points.

Regards
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35000612
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
 

Author Comment

by:bruno_boccara
ID: 35000837
at least I will give 250 points to each of you for your help !

I ask for the moderator ....
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 35003048
@bportlock:

Just to be clear that I wasn't complaining. :)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35004876
@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
 

Author Closing Comment

by:bruno_boccara
ID: 35004929
Thanks to R-Byter and to bportlock for your help !!!
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 35004958
You're welcome.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question