Solved

str_replace not accepting a number of times parameter

Posted on 2006-11-09
11
207 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
  • 4
  • 4
  • 3
11 Comments
 
LVL 16

Expert Comment

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

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

Author Comment

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

Expert Comment

by:HackneyCab
Comment Utility
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
 
LVL 48

Expert Comment

by:hernst42
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

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

Expert Comment

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

Author Comment

by:jackjohnson44
Comment Utility
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
Comment Utility
then use

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

Author Comment

by:jackjohnson44
Comment Utility
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 500 total points
Comment Utility
Is the following not working?

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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 dynamically set the form action using jQuery.

762 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

7 Experts available now in Live!

Get 1:1 Help Now