Solved

STR_REPLACE TROUBLE

Posted on 2011-02-28
13
402 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
 

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now