Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

str_replace not accepting a number of times parameter

Posted on 2006-11-09
11
Medium Priority
?
218 Views
Last Modified: 2008-03-17
I thought you could limit the number of times str_replace replaces your search string

str_replace("A", "", "ABCDEFGABCDEFG")

gives me

BCDEFBCDEF

I only want the first instance to be replaced

str_replace("A", "", "ABCDEFGABCDEFG", 1)
BCDEFABCDEF
This is giving me the error:
Wrong parameter count for str_replace()

I am not sure which version of php I am using.
0
Comment
Question by:jackjohnson44
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 16

Expert Comment

by:HackneyCab
ID: 17910956
No, but you can set a limit if you use preg_replace() instead.

preg_replace('A', '', 'ABCDEFGABCDEFG', 1);
0
 

Author Comment

by:jackjohnson44
ID: 17910960
will it matter if I am using weird characters?
< > : ; .
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 17911182
That's a very non-specific question.

Yes, regular expressions are very sensitive to the pattern you supply.

If you only want to extract the stuff before and after a string, then perhaps you're better off using functions like strpos() and substr() to extract before and after the string that you are trying to banish.

If you're more specific, that will help to form a solution.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 48

Expert Comment

by:hernst42
ID: 17912724
the preg_preplace is not correct. There are the delimiters missing Try

$search = "A";
preg_replace('/'.preg_quote($search, '/') . '/', '', $text, 1);

This will work independend of the character you want to replace
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 17914039
You're right, I always forget the pattern delimiters the first time I write a preg_replace statement. Your statement is a much more useful one.
0
 

Author Comment

by:jackjohnson44
ID: 17914410
Thanks, will that work for the search string and the replace string?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 17914521
Yup will work for both, as long aus you don't have \ in your replacement string
0
 

Author Comment

by:jackjohnson44
ID: 17914541
What if I do?

I can't guarantee that, it is from a form.

Is there a way to treat search and replace as literals and only replace the first instance?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 17914725
then use

preg_replace('/'.preg_quote($search, '/') . '/', str_replace("\\", "\\\\", $replacement), $text, 1);
0
 

Author Comment

by:jackjohnson44
ID: 17935018
thanks for your help, but I probably should have been more specific.
Here are my delimiters that will be in search and replace.

one is a colon and one a semi colon surrounded with <  > 
<:>  
<;>
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 17936765
Is the following not working?

$search = '<:>';
$replacement = '<;>';
$text = preg_replace('/'.preg_quote($search, '/') . '/', str_replace("\\", "\\\\", $replacement), $text, 1);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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 …
Suggested Courses

609 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