Solved

STR_REPLACE TROUBLE

Posted on 2011-02-28
13
419 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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