Re: str_replace does not like empty quotes [message #186192 is a reply to message #186191] |
Fri, 20 June 2014 00:55 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 6/19/2014 8:11 PM, Lew Pitcher wrote:
> On Thursday 19 June 2014 19:33, in comp.lang.php, "richard"
> <noreply(at)example(dot)com> wrote:
>
>> $a=str_replace($a,"\","");
>>
>> This generates the error: Unexpected "".
>
> Of course it does. You are trying to replace all the strings that match $a
> with "\" in an empty string ("").
>
>> http://www.php.net//manual/en/function.str-replace.php
>>
>> While here in the manual, they do precisely the same thing!
>
> To quote the webpage:
> # str_replace — Replace all occurrences of the search string with the
> # replacement string
> # Description
> # mixed str_replace ( mixed $search , mixed $replace , mixed $subject
> # [, int &$count ] )
> #
> # This function returns a string or an array with all occurrences of search
> # in subject replaced with the given replace value.
>
> Note that "search" is the first parameter, "replace" is the second,
> and "subject" is the third.
>
> So, your
> str_replace($a,"\","");
> will return a string or array with all occurrences of
> the contents of $a
> in
> ""
> with the given
> "\"
> value
>
>
> What you apparently want is
> str_replace("\","",$a);
>
>> <quote>
>> If search and replace are arrays, then str_replace() takes a value from
>> each array and uses them to search and replace on subject. If replace has
>> fewer values than search, then an empty string is used for the rest of
>> replacement values. If search is an array and replace is a string, then
>> this replacement string is used for every value of search. The converse
>> would not make sense, though.
>> </quote>
>
>
Lew,
Not quite. Hint: think escape character...
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
|
|
|