Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

str_replace not accepting a number of times parameter

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
jackjohnson44
Asked:
jackjohnson44
  • 4
  • 4
  • 3
1 Solution
 
HackneyCabCommented:
No, but you can set a limit if you use preg_replace() instead.

preg_replace('A', '', 'ABCDEFGABCDEFG', 1);
0
 
jackjohnson44Author Commented:
will it matter if I am using weird characters?
< > : ; .
0
 
HackneyCabCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
hernst42Commented:
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
 
HackneyCabCommented:
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
 
jackjohnson44Author Commented:
Thanks, will that work for the search string and the replace string?
0
 
hernst42Commented:
Yup will work for both, as long aus you don't have \ in your replacement string
0
 
jackjohnson44Author Commented:
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
 
hernst42Commented:
then use

preg_replace('/'.preg_quote($search, '/') . '/', str_replace("\\", "\\\\", $replacement), $text, 1);
0
 
jackjohnson44Author Commented:
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
 
hernst42Commented:
Is the following not working?

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now