Solved

STR_REPLACE TROUBLE

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 dynamically set the form action using jQuery.

710 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