FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » str_replace does not like empty quotes
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
str_replace does not like empty quotes [message #186189] Thu, 19 June 2014 23:33 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
$a=str_replace($a,"\","");

This generates the error: Unexpected "".

http://www.php.net//manual/en/function.str-replace.php

While here in the manual, they do precisely the same thing!

<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>
Re: str_replace does not like empty quotes [message #186190 is a reply to message #186189] Fri, 20 June 2014 00:09 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/19/2014 7:33 PM, richard wrote:
> $a=str_replace($a,"\","");
>
> This generates the error: Unexpected "".
>
> http://www.php.net//manual/en/function.str-replace.php
>
> While here in the manual, they do precisely the same thing!
>
> <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>
>

str_replace() works fine with empty quotes. That's not your problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186191 is a reply to message #186189] Fri, 20 June 2014 00:11 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
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 Pitcher
"In Skills, We Trust"
PGP public key available upon request
Re: str_replace does not like empty quotes [message #186192 is a reply to message #186191] Fri, 20 June 2014 00:55 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: str_replace does not like empty quotes [message #186193 is a reply to message #186189] Fri, 20 June 2014 01:19 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
richard, 2014-06-20 01:33:

> $a=str_replace($a,"\","");
>
> This generates the error: Unexpected "".
>
> http://www.php.net//manual/en/function.str-replace.php
>
> While here in the manual, they do precisely the same thing!

No - they don't!

Use: $a=str_replace($a,"\"","");

Beware of the double quotes, since \ starts an escape - this means, the
meaning of the second quote (") is NOT "end string here".

To make this more clear:


" <- String starts here. \" <- This just ONE character end here -> "


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de
Re: str_replace does not like empty quotes [message #186194 is a reply to message #186193] Fri, 20 June 2014 01:21 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Arno Welzel, 2014-06-20 03:19:

> richard, 2014-06-20 01:33:
>
>> $a=str_replace($a,"\","");
>>
>> This generates the error: Unexpected "".
>>
>> http://www.php.net//manual/en/function.str-replace.php
>>
>> While here in the manual, they do precisely the same thing!
>
> No - they don't!
>
> Use: $a=str_replace($a,"\"","");

Sorry - of course: $a=str_replace("\"","",$a);

Since the order of the parameters is

1) What to search
2) What to replace
3) Where to search in

> Beware of the double quotes, since \ starts an escape - this means, the
> meaning of the second quote (") is NOT "end string here".
>
> To make this more clear:
>
>
> " <- String starts here. \" <- This just ONE character end here -> "


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de
Re: str_replace does not like empty quotes [message #186195 is a reply to message #186194] Fri, 20 June 2014 01:33 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/19/2014 9:21 PM, Arno Welzel wrote:
> Arno Welzel, 2014-06-20 03:19:
>
>> richard, 2014-06-20 01:33:
>>
>>> $a=str_replace($a,"\","");
>>>
>>> This generates the error: Unexpected "".
>>>
>>> http://www.php.net//manual/en/function.str-replace.php
>>>
>>> While here in the manual, they do precisely the same thing!
>>
>> No - they don't!
>>
>> Use: $a=str_replace($a,"\"","");
>
> Sorry - of course: $a=str_replace("\"","",$a);
>
> Since the order of the parameters is
>
> 1) What to search
> 2) What to replace
> 3) Where to search in
>
>> Beware of the double quotes, since \ starts an escape - this means, the
>> meaning of the second quote (") is NOT "end string here".
>>
>> To make this more clear:
>>
>>
>> " <- String starts here. \" <- This just ONE character end here -> "
>
>

Arno, that will replace the double quotes ("), which doesn't look like
what Richard wants.

But why help him when all he does is insult the same people who try to
help him? That's exactly why I didn't just give him the answer in the
first place (not to mention the fact he's asked similar questions
before, and has still failed to learn).

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186196 is a reply to message #186189] Fri, 20 June 2014 01:59 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 19 Jun 2014 19:33:56 -0400, richard wrote:

> $a=str_replace($a,"\","");
> This generates the error: Unexpected "".

Yes, it would.

> http://www.php.net//manual/en/function.str-replace.php
> While here in the manual, they do precisely the same thing!

No they don't.

To be more specific, nowhere on that page is an example that contains

/*_precisely the same_*/

code and strings as you have used.

There is a reason that you have been advised in the past to use certain
functions and best practices instead of trying to reinvent them yourself.
This is just another example of your persistent refusal to listen to good
advice biting your arse.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: str_replace does not like empty quotes [message #186197 is a reply to message #186195] Fri, 20 June 2014 02:06 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Jerry Stuckle, 2014-06-20 03:33:

> On 6/19/2014 9:21 PM, Arno Welzel wrote:
>> Arno Welzel, 2014-06-20 03:19:
>>
>>> richard, 2014-06-20 01:33:
>>>
>>>> $a=str_replace($a,"\","");
>>>>
>>>> This generates the error: Unexpected "".
>>>>
>>>> http://www.php.net//manual/en/function.str-replace.php
>>>>
>>>> While here in the manual, they do precisely the same thing!
>>>
>>> No - they don't!
>>>
>>> Use: $a=str_replace($a,"\"","");
>>
>> Sorry - of course: $a=str_replace("\"","",$a);
>>
>> Since the order of the parameters is
>>
>> 1) What to search
>> 2) What to replace
>> 3) Where to search in
>>
>>> Beware of the double quotes, since \ starts an escape - this means, the
>>> meaning of the second quote (") is NOT "end string here".
>>>
>>> To make this more clear:
>>>
>>>
>>> " <- String starts here. \" <- This just ONE character end here -> "
>>
>>
>
> Arno, that will replace the double quotes ("), which doesn't look like
> what Richard wants.

Well - I don't know, what Richard really wants. If it was about removing
backslashes:

$a=str_replace("\\","",$a);

> But why help him when all he does is insult the same people who try to
> help him? That's exactly why I didn't just give him the answer in the
> first place (not to mention the fact he's asked similar questions
> before, and has still failed to learn).

I don't want to give newbies the impression, that Richard knows, what he
is writing about, when he claims that things he does are exactly shown
like that in the PHP manual ;-)


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de
Re: str_replace does not like empty quotes [message #186198 is a reply to message #186193] Fri, 20 June 2014 02:13 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Fri, 20 Jun 2014 03:19:53 +0200, Arno Welzel wrote:

> richard, 2014-06-20 01:33:
>
>> $a=str_replace($a,"\","");
>>
>> This generates the error: Unexpected "".
>>
>> http://www.php.net//manual/en/function.str-replace.php
>>
>> While here in the manual, they do precisely the same thing!
>
> No - they don't!
>
> Use: $a=str_replace($a,"\"","");
>
> Beware of the double quotes, since \ starts an escape - this means, the
> meaning of the second quote (") is NOT "end string here".
>
> To make this more clear:
>
>
> " <- String starts here. \" <- This just ONE character end here -> "

That could be the problem as some times the \ shows and some times it
doesn't.
I've tried several combinatiosn, still the \ remains.

As for the technically correct proper order it is
search, replace, subject.
Re: str_replace does not like empty quotes [message #186199 is a reply to message #186197] Fri, 20 June 2014 02:26 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/19/2014 10:06 PM, Arno Welzel wrote:
> Jerry Stuckle, 2014-06-20 03:33:
>
>> On 6/19/2014 9:21 PM, Arno Welzel wrote:
>>> Arno Welzel, 2014-06-20 03:19:
>>>
>>>> richard, 2014-06-20 01:33:
>>>>
>>>> > $a=str_replace($a,"\","");
>>>> >
>>>> > This generates the error: Unexpected "".
>>>> >
>>>> > http://www.php.net//manual/en/function.str-replace.php
>>>> >
>>>> > While here in the manual, they do precisely the same thing!
>>>>
>>>> No - they don't!
>>>>
>>>> Use: $a=str_replace($a,"\"","");
>>>
>>> Sorry - of course: $a=str_replace("\"","",$a);
>>>
>>> Since the order of the parameters is
>>>
>>> 1) What to search
>>> 2) What to replace
>>> 3) Where to search in
>>>
>>>> Beware of the double quotes, since \ starts an escape - this means, the
>>>> meaning of the second quote (") is NOT "end string here".
>>>>
>>>> To make this more clear:
>>>>
>>>>
>>>> " <- String starts here. \" <- This just ONE character end here -> "
>>>
>>>
>>
>> Arno, that will replace the double quotes ("), which doesn't look like
>> what Richard wants.
>
> Well - I don't know, what Richard really wants. If it was about removing
> backslashes:
>
> $a=str_replace("\\","",$a);
>
>> But why help him when all he does is insult the same people who try to
>> help him? That's exactly why I didn't just give him the answer in the
>> first place (not to mention the fact he's asked similar questions
>> before, and has still failed to learn).
>
> I don't want to give newbies the impression, that Richard knows, what he
> is writing about, when he claims that things he does are exactly shown
> like that in the PHP manual ;-)
>
>

I don't think even newbies would consider richard knows what he's doing
here...


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186200 is a reply to message #186191] Fri, 20 June 2014 03:23 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
Lew Pitcher <lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote in news:J_Kov.32084$LR3(dot)8021(at)fx28(dot)iad:

> 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 ("").

I thought you plonked him a couple days ago...
Re: str_replace does not like empty quotes [message #186201 is a reply to message #186189] Fri, 20 June 2014 03:25 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:1kcemjreijezv$.k2q3x4bl6eoc.dlg@
40tude.net:

> $a=str_replace($a,"\","");
>
> This generates the error: Unexpected "".
>
> http://www.php.net//manual/en/function.str-replace.php
>
> While here in the manual, they do precisely the same thing!

No, they do not. RTFM, RtS.

<quote>
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.
</quote>

Now look at what you wrote, and compare it to the description in the manual.
Re: str_replace does not like empty quotes [message #186202 is a reply to message #186198] Fri, 20 June 2014 03:26 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:7heper378zqa.dovihixf4du2.dlg@
40tude.net:

> On Fri, 20 Jun 2014 03:19:53 +0200, Arno Welzel wrote:
>
>> richard, 2014-06-20 01:33:
>>
>>> $a=str_replace($a,"\","");
>>>
>>> This generates the error: Unexpected "".
>>>
>>> http://www.php.net//manual/en/function.str-replace.php
>>>
>>> While here in the manual, they do precisely the same thing!
>>
>> No - they don't!
>>
>> Use: $a=str_replace($a,"\"","");
>>
>> Beware of the double quotes, since \ starts an escape - this means, the
>> meaning of the second quote (") is NOT "end string here".
>>
>> To make this more clear:
>>
>>
>> " <- String starts here. \" <- This just ONE character end here -> "
>
> That could be the problem as some times the \ shows and some times it
> doesn't.
> I've tried several combinatiosn, still the \ remains.
>
> As for the technically correct proper order it is
> search, replace, subject.

Indeed it is. But that's not what you're doing. And that's why it doesn't work.
Re: str_replace does not like empty quotes [message #186203 is a reply to message #186189] Fri, 20 June 2014 06:10 Go to previous messageGo to next message
Geoff Muldoon is currently offline  Geoff Muldoon
Messages: 19
Registered: July 2011
Karma: 0
Junior Member
In article <1kcemjreijezv$(dot)k2q3x4bl6eoc(dot)dlg(at)40tude(dot)net>,
noreply(at)example(dot)com says...

> $a=str_replace($a,"\","");

Richard:

a) You need to learn to read the documentation syntax order properly, $a
goes at the end, not the start.

b) Just because you find a hammer, everything doesn't become a nail.
Please try and use the RIGHT tool for the job. Do not mindlessly expect
silver bullets to appear, especially while looking down a barrel. Do
some proper research for yourself first.

c) Google PHP stripslashes.

Geoff M
Re: str_replace does not like empty quotes [message #186204 is a reply to message #186191] Fri, 20 June 2014 07:31 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
[Cancel & Supersedes]

Lew Pitcher wrote:

> On Thursday 19 June 2014 19:33, in comp.lang.php, "richard"
> <noreply(at)example(dot)com> wrote:

It is called attribution *line*, _not_ attribution novel.

>> $a=str_replace($a,"\","");
'---'
>> This generates the error: Unexpected "".

The *actual* error message is

,----
| $ php -r '$a=str_replace($a,"\","");'
| PHP Parse error: syntax error, unexpected '"' in Command line code on
^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^
| line 1
`----

with

,----
| $ php -v
| PHP 5.6.0beta4 (cli) (built: Jun 10 2014 16:51:46)
| Copyright (c) 1997-2014 The PHP Group
| Zend Engine v2.6.0-dev, Copyright (c) 1998-2014 Zend Technologies
| with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend
| Technologies
| with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans
`----

Note the *single-quoted* double-quote in the error message.

> Of course it does. You are trying to replace all the strings that match $a
> with "\" in an empty string ("").

No, he does not. The *main* error here is a *syntax* error. I have marked
the string literal in the statement above (as always, use a fixed-width
font), so that you can see the trailing <"> better.

Note the *escaped* double-quote in the string literal. Note where the
string literal starts and where it therefore ends, which syntax-highlighting
shows in any decent PHP editor (such as Eclipse PDT).


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
Re: str_replace does not like empty quotes [message #186205 is a reply to message #186198] Fri, 20 June 2014 09:01 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
richard, 2014-06-20 04:13:

> On Fri, 20 Jun 2014 03:19:53 +0200, Arno Welzel wrote:
>
>> richard, 2014-06-20 01:33:
>>
>>> $a=str_replace($a,"\","");
>>>
>>> This generates the error: Unexpected "".
>>>
>>> http://www.php.net//manual/en/function.str-replace.php
>>>
>>> While here in the manual, they do precisely the same thing!
>>
>> No - they don't!
>>
>> Use: $a=str_replace($a,"\"","");
>>
>> Beware of the double quotes, since \ starts an escape - this means, the
>> meaning of the second quote (") is NOT "end string here".
>>
>> To make this more clear:
>>
>>
>> " <- String starts here. \" <- This just ONE character end here -> "
>
> That could be the problem as some times the \ shows and some times it
> doesn't.
> I've tried several combinatiosn, still the \ remains.
>
> As for the technically correct proper order it is
> search, replace, subject.

No - it isn't - READ:

<http://www.php.net//manual/en/function.str-replace.php>


str_replace ( $search , $replace , $subject [, int &$count ] )

This function returns a string or an array with all occurrences of
search in subject replaced with the given replace value.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de
Re: str_replace does not like empty quotes [message #186206 is a reply to message #186205] Fri, 20 June 2014 09:02 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Arno Welzel, 2014-06-20 11:01:

> richard, 2014-06-20 04:13:
>
>> On Fri, 20 Jun 2014 03:19:53 +0200, Arno Welzel wrote:
>>
>>> richard, 2014-06-20 01:33:
>>>
>>>> $a=str_replace($a,"\","");
>>>>
>>>> This generates the error: Unexpected "".
>>>>
>>>> http://www.php.net//manual/en/function.str-replace.php
>>>>
>>>> While here in the manual, they do precisely the same thing!
>>>
>>> No - they don't!
>>>
>>> Use: $a=str_replace($a,"\"","");
>>>
>>> Beware of the double quotes, since \ starts an escape - this means, the
>>> meaning of the second quote (") is NOT "end string here".
>>>
>>> To make this more clear:
>>>
>>>
>>> " <- String starts here. \" <- This just ONE character end here -> "
>>
>> That could be the problem as some times the \ shows and some times it
>> doesn't.
>> I've tried several combinatiosn, still the \ remains.
>>
>> As for the technically correct proper order it is
>> search, replace, subject.
>
> No - it isn't - READ:
>
> <http://www.php.net//manual/en/function.str-replace.php>
>
>
> str_replace ( $search , $replace , $subject [, int &$count ] )
>
> This function returns a string or an array with all occurrences of
> search in subject replaced with the given replace value.

Sorry... Richard is really confusing to read sometimes - since he did
read the manual but did not use it...


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de
Re: str_replace does not like empty quotes [message #186208 is a reply to message #186203] Fri, 20 June 2014 11:48 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Geoff Muldoon wrote:

> In article <1kcemjreijezv$(dot)k2q3x4bl6eoc(dot)dlg(at)40tude(dot)net>,
> noreply(at)example(dot)com says...
>
>> $a=str_replace($a,"\","");
>
> c) Google PHP stripslashes.

Good point! However, the need for removing backslashes begs the
question where these backslashes come from in the first place. I
suppose the cause is <news:itrvizyykmyu$(dot)1m8xc48h8xvgi(dot)dlg(at)40tude(dot)net>:

| $a=str_replace("'","\'",$a);
| Works just fine.

So much for "works just fine".

--
Christoph M. Becker
Re: str_replace does not like empty quotes [message #186213 is a reply to message #186198] Fri, 20 June 2014 15:32 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 19 Jun 2014 22:13:00 -0400, richard wrote:

> As for the technically correct proper order it is search, replace,
> subject.

It may be, but you do not get to redefine the terms search, replace and
subject to mean whatever you think they ought to mean, you have to
respect the definitions that the documentation uses.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: str_replace does not like empty quotes [message #186214 is a reply to message #186206] Fri, 20 June 2014 15:38 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Fri, 20 Jun 2014 11:02:32 +0200, Arno Welzel wrote:

> Sorry... Richard is really confusing to read sometimes - since he did
> read the manual but did not use it...

Nonono - he did actually read the manual, and then he redefined all the
words it used to some richardian alternatives.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: str_replace does not like empty quotes [message #186215 is a reply to message #186208] Fri, 20 June 2014 15:40 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Fri, 20 Jun 2014 13:48:24 +0200, Christoph Michael Becker wrote:

> Geoff Muldoon wrote:
>
>> In article <1kcemjreijezv$(dot)k2q3x4bl6eoc(dot)dlg(at)40tude(dot)net>,
>> noreply(at)example(dot)com says...
>>
>>> $a=str_replace($a,"\","");
>>
>> c) Google PHP stripslashes.
>
> Good point! However, the need for removing backslashes begs the
> question where these backslashes come from in the first place. I
> suppose the cause is <news:itrvizyykmyu$(dot)1m8xc48h8xvgi(dot)dlg(at)40tude(dot)net>:
>
> | $a=str_replace("'","\'",$a);
> | Works just fine.
>
> So much for "works just fine".

As I said upthread, to richard:

There is a reason that you have been advised in the past to use certain
functions and best practices instead of trying to reinvent them yourself.
This is just another example of your persistent refusal to listen to good
advice biting your arse.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: str_replace does not like empty quotes [message #186258 is a reply to message #186208] Sun, 22 June 2014 00:05 Go to previous messageGo to next message
Geoff Muldoon is currently offline  Geoff Muldoon
Messages: 19
Registered: July 2011
Karma: 0
Junior Member
cmbecker69(at)arcor(dot)de says...
>
> Geoff Muldoon wrote:

>> c) Google PHP stripslashes.
>
> Good point! However, the need for removing backslashes begs the
> question where these backslashes come from in the first place.

Probably depends amongst other things on whether the database insert
facility uses strings or bind variables.

Of course, bind variables are massively better, but I doubt that Richard
is up to knowing about them yet.

GM
Re: str_replace does not like empty quotes [message #186259 is a reply to message #186258] Sun, 22 June 2014 00:08 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/21/2014 8:05 PM, Geoff Muldoon wrote:
> cmbecker69(at)arcor(dot)de says...
>>
>> Geoff Muldoon wrote:
>
>>> c) Google PHP stripslashes.
>>
>> Good point! However, the need for removing backslashes begs the
>> question where these backslashes come from in the first place.
>
> Probably depends amongst other things on whether the database insert
> facility uses strings or bind variables.
>

Makes no difference whether strings or bind values are used. Properly
processed, both work equally well in a database.

> Of course, bind variables are massively better, but I doubt that Richard
> is up to knowing about them yet.
>
> GM
>

One opinion. But bound values have their problems, also. They are
neither better nor worse than properly escaping values. Just another
way of doing things.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186282 is a reply to message #186259] Sun, 22 June 2014 23:20 Go to previous messageGo to next message
Geoff Muldoon is currently offline  Geoff Muldoon
Messages: 19
Registered: July 2011
Karma: 0
Junior Member
jstucklex(at)attglobal(dot)net says...

>> Of course, bind variables are massively better, but I doubt that
Richard
>> is up to knowing about them yet.

> One opinion. But bound values have their problems, also. They are
> neither better nor worse than properly escaping values. Just another
> way of doing things.

Hmm, I've never really found any signifcant down-side to using bind
variables if they are used corrctly, so I'd appreciate any links you
might have.

IMHO three of the main pluses are:

a) A strong (but not totally foolproof) barrier against SQL injection
attacks.

b) On certain platforms (perhaps on Oracle more so than MySQL, etc.) the
ability to shared pool cache or soft-parse rather than hard-parse a
particular execution plan, often significantly increasing performance
when querying the same columns from the sames tables.

c) Parameter input values do not NEED to be properly escaped.

http://en.wikipedia.org/wiki/Prepared_statement

GM
Re: str_replace does not like empty quotes [message #186283 is a reply to message #186282] Mon, 23 June 2014 00:44 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/22/2014 7:20 PM, Geoff Muldoon wrote:
> jstucklex(at)attglobal(dot)net says...
>
>>> Of course, bind variables are massively better, but I doubt that
> Richard
>>> is up to knowing about them yet.
>
>> One opinion. But bound values have their problems, also. They are
>> neither better nor worse than properly escaping values. Just another
>> way of doing things.
>
> Hmm, I've never really found any signifcant down-side to using bind
> variables if they are used corrctly, so I'd appreciate any links you
> might have.
>
> IMHO three of the main pluses are:
>
> a) A strong (but not totally foolproof) barrier against SQL injection
> attacks.
>
> b) On certain platforms (perhaps on Oracle more so than MySQL, etc.) the
> ability to shared pool cache or soft-parse rather than hard-parse a
> particular execution plan, often significantly increasing performance
> when querying the same columns from the sames tables.
>
> c) Parameter input values do not NEED to be properly escaped.
>
> http://en.wikipedia.org/wiki/Prepared_statement
>
> GM
>

Performance, for one. Bound values require at least one extra call to
the libraries (for the PREPARE).

Additional calls are required to bind numeric values. Of course they
are also required for string variables, but since strings need to
otherwise be processed through mysql_real_escape_string(), that's a push.

Additionally, values should always be validated before being used in a
mysql request anyway.

The extra processing can be excessive in a busy site. Of course, if
you're only getting 100 hits/day, it's not a problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186294 is a reply to message #186283] Mon, 23 June 2014 06:33 Go to previous messageGo to next message
Geoff Muldoon is currently offline  Geoff Muldoon
Messages: 19
Registered: July 2011
Karma: 0
Junior Member
jstucklex(at)attglobal(dot)net says...
>
> On 6/22/2014 7:20 PM, Geoff Muldoon wrote:
>> jstucklex(at)attglobal(dot)net says...

>> Hmm, I've never really found any signifcant down-side to using bind
>> variables if they are used corrctly, so I'd appreciate any links you
>> might have.

> Performance, for one. Bound values require at least one extra call to
> the libraries (for the PREPARE).

> The extra processing can be excessive in a busy site. Of course, if
> you're only getting 100 hits/day, it's not a problem.

Yes, the first usage of a prepared statement will always require that
extra call, and if it is a query that is unlikely to be run again before
it is flushed from the cache, it will require this call again each time.

However, *particularly if it IS a busy site* and it is a query that is
likely to be executed with a very high regularity (e.g.: "select <fixed
list of order details columns> from order_details_table where order_id =
:<bound variable>") then the benefit of SQL execution plan caching and
soft instead of hard parsing will usually *massively* outweigh that
possible initial overhead.

High-volume sites are MORE likely than toy ones to benefit from bind
variables.

GM
Re: str_replace does not like empty quotes [message #186299 is a reply to message #186294] Mon, 23 June 2014 13:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/23/2014 2:33 AM, Geoff Muldoon wrote:
> jstucklex(at)attglobal(dot)net says...
>>
>> On 6/22/2014 7:20 PM, Geoff Muldoon wrote:
>>> jstucklex(at)attglobal(dot)net says...
>
>>> Hmm, I've never really found any signifcant down-side to using bind
>>> variables if they are used corrctly, so I'd appreciate any links you
>>> might have.
>
>> Performance, for one. Bound values require at least one extra call to
>> the libraries (for the PREPARE).
>
>> The extra processing can be excessive in a busy site. Of course, if
>> you're only getting 100 hits/day, it's not a problem.
>
> Yes, the first usage of a prepared statement will always require that
> extra call, and if it is a query that is unlikely to be run again before
> it is flushed from the cache, it will require this call again each time.
>
> However, *particularly if it IS a busy site* and it is a query that is
> likely to be executed with a very high regularity (e.g.: "select <fixed
> list of order details columns> from order_details_table where order_id =
> :<bound variable>") then the benefit of SQL execution plan caching and
> soft instead of hard parsing will usually *massively* outweigh that
> possible initial overhead.
>
> High-volume sites are MORE likely than toy ones to benefit from bind
> variables.
>
> GM
>

If it's a busy site, then there will be a lot more calls to MySQL - and
chances are the cache will be filled (and flushed) before the prepared
statement is run again. It's not how long since the query has run -
it's how many other queries have been run. Remember it isn't just one
query being run on a site - a large site will have hundreds (or more)
different queries.

And it will still require a call each time, because you never know
whether it has been flushed or not.

Additionally, you ignore the fact additional calls need to be made to
bind numeric values - calls which are not required when not using bind
values.

Have you ever worked on a high-active site? Say 1K hits/second or more?
And have you ever profiled bind values on such a site? I have. Bind
values are significantly slower.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186309 is a reply to message #186299] Tue, 24 June 2014 02:34 Go to previous messageGo to next message
Geoff Muldoon is currently offline  Geoff Muldoon
Messages: 19
Registered: July 2011
Karma: 0
Junior Member
jstucklex(at)attglobal(dot)net says...
> Have you ever worked on a high-active site? Say 1K hits/second or
more?
> And have you ever profiled bind values on such a site? I have.

Most high-activity sites I've worked on have used Oracle rather than
MySQL, and Oracle is much better at targetting and maintaining bind
variable execution plans in cache. You may be right that MySQL gains
less and could suffer more from using them.

Additionally, just because a site has high volumes that doesn't
necessarily mean lots of different SQL statements. I've worked on very
high volume sites where 80+% of the database calls are made using less
than 10 different queries - find a product, add product to order
(looping), show an order, write an invoice, etc. In these cases, with
correct cache size tweaking, performace was substantially improved by
using bind variables.

GM
Re: str_replace does not like empty quotes [message #186310 is a reply to message #186309] Tue, 24 June 2014 03:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/23/2014 10:34 PM, Geoff Muldoon wrote:
> jstucklex(at)attglobal(dot)net says...
>> Have you ever worked on a high-active site? Say 1K hits/second or
> more?
>> And have you ever profiled bind values on such a site? I have.
>
> Most high-activity sites I've worked on have used Oracle rather than
> MySQL, and Oracle is much better at targetting and maintaining bind
> variable execution plans in cache. You may be right that MySQL gains
> less and could suffer more from using them.
>
> Additionally, just because a site has high volumes that doesn't
> necessarily mean lots of different SQL statements. I've worked on very
> high volume sites where 80+% of the database calls are made using less
> than 10 different queries - find a product, add product to order
> (looping), show an order, write an invoice, etc. In these cases, with
> correct cache size tweaking, performace was substantially improved by
> using bind variables.
>
> GM
>

It depends on the site. I've also used Oracle on websites (as well as
DB2 and SQL Server). Results vary quite a bit.

I've never seen a site where 80+% of the database calls are made with
less than 10 different queries. But then the sites I've worked on are
typically pretty complicated sites - not simple shopping carts. If all
you're doing is a shopping cart, then I can see where bind variables
might help.

But just because you find something works better in Oracle doesn't mean
it works better in MySQL. I find a lot of things work better in DB2,
for instance, than either Oracle or MySQL (try doing recursive SQL in
MySQL, for instance).

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186311 is a reply to message #186189] Tue, 24 June 2014 06:13 Go to previous messageGo to next message
Olaf S. is currently offline  Olaf S.
Messages: 10
Registered: December 2011
Karma: 0
Junior Member
Am 20.06.2014 01:33, schrieb richard:
> $a=str_replace($a,"\","");
$a=str_replace($a,"\\","");
Re: str_replace does not like empty quotes [message #186312 is a reply to message #186311] Tue, 24 June 2014 08:44 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Am 2014-06-24 08:13, schrieb Olaf Schmitt:

> Am 20.06.2014 01:33, schrieb richard:
>> $a=str_replace($a,"\","");
> $a=str_replace($a,"\\","");

JFTR: The order is search, replace, subject and not subject, search,
replace.

So the right solution is:

$a=str_replace("\\", "", $a);

Or just use:

$a = stripslashes($a);

;-)


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de
Re: str_replace does not like empty quotes [message #186313 is a reply to message #186309] Tue, 24 June 2014 11:12 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
Geoff Muldoon <geoff(dot)muldoon(at)trap(dot)gmail(dot)com> wrote in news:MPG.2e138ecff04eddb9d1
@news.albasani.net:

[...]
> Additionally, just because a site has high volumes that doesn't
> necessarily mean lots of different SQL statements.

I would argue just the opposite, in fact -- the higher the volume, the more likely that a large
number of SQL statements are substantially the same.

> I've worked on very
> high volume sites where 80+% of the database calls are made using less
> than 10 different queries - find a product, add product to order
> (looping), show an order, write an invoice, etc. In these cases, with
> correct cache size tweaking, performace was substantially improved by
> using bind variables.

Exactly so.
Re: str_replace does not like empty quotes [message #186315 is a reply to message #186313] Tue, 24 June 2014 12:01 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/24/2014 7:12 AM, Doug Miller wrote:
> Geoff Muldoon <geoff(dot)muldoon(at)trap(dot)gmail(dot)com> wrote in news:MPG.2e138ecff04eddb9d1
> @news.albasani.net:
>
> [...]
>> Additionally, just because a site has high volumes that doesn't
>> necessarily mean lots of different SQL statements.
>
> I would argue just the opposite, in fact -- the higher the volume, the more likely that a large
> number of SQL statements are substantially the same.
>
>> I've worked on very
>> high volume sites where 80+% of the database calls are made using less
>> than 10 different queries - find a product, add product to order
>> (looping), show an order, write an invoice, etc. In these cases, with
>> correct cache size tweaking, performace was substantially improved by
>> using bind variables.
>
> Exactly so.
>

The number of SQL statements is not based on the volume; rather it is
based on what the site does. If all you have is a shopping cart, the
number of SQL statements is going to be low. Other sites have a much
larger number of SQL statements.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: str_replace does not like empty quotes [message #186320 is a reply to message #186312] Tue, 24 June 2014 18:48 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 24 Jun 2014 10:44:51 +0200, Arno Welzel wrote:

> Am 2014-06-24 08:13, schrieb Olaf Schmitt:
>
>> Am 20.06.2014 01:33, schrieb richard:
>>> $a=str_replace($a,"\","");
>> $a=str_replace($a,"\\","");
>
> JFTR: The order is search, replace, subject and not subject, search,
> replace.

Yes, but richard is using his own definitions for search, replace and
subject, which only align with the php in the 42nd alternative universe
across.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: str_replace does not like empty quotes [message #186321 is a reply to message #186320] Tue, 24 June 2014 19:53 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Denis McMahon wrote:

> On Tue, 24 Jun 2014 10:44:51 +0200, Arno Welzel wrote:
>
>> Am 2014-06-24 08:13, schrieb Olaf Schmitt:
>>
>>> Am 20.06.2014 01:33, schrieb richard:
>>>> $a=str_replace($a,"\","");
>>> $a=str_replace($a,"\\","");
>>
>> JFTR: The order is search, replace, subject and not subject, search,
>> replace.
>
> Yes, but richard is using his own definitions for search, replace and
> subject, which only align with the php in the 42nd alternative universe
> across.

Or maybe he's doing something like the following:

runkit_function_rename('str_replace', 'php_str_replace');
runkit_function_add(
'str_replace', '$subject, $search, $replace',
'return php_str_replace($search, $replace, $subject);'
);

echo str_replace('something', 'some', 'any');

SCNR.

--
Christoph M. Becker
Re: str_replace does not like empty quotes [message #186322 is a reply to message #186321] Tue, 24 June 2014 20:08 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Christoph Michael Becker wrote:

> Denis McMahon wrote:
>> Yes, but richard is using his own definitions for search, replace and
>> subject, which only align with the php in the 42nd alternative universe
>> across.
>
> Or maybe he's doing something like the following:
>
> runkit_function_rename('str_replace', 'php_str_replace');
> runkit_function_add(
> 'str_replace', '$subject, $search, $replace',
> 'return php_str_replace($search, $replace, $subject);'
> );
>
> echo str_replace('something', 'some', 'any');
>
> SCNR.

He must not forget runkit.internal_override=1 in that case ;-)


Pointed"YMMD"Ears
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PDO fetch with SQL server
Next Topic: Putting it all together
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 22 04:53:37 GMT 2024

Total time taken to generate the page: 0.02683 seconds