Solved

STR_REPLACE TROUBLE

Posted on 2011-02-28
13
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 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