Skip to content
Advertisement

Replacing “/ characters with sed, from within php

I generate password from php (thru web page) using shell_exec / dd/ /dev/urandom. I want to eliminate
"/ characters from my generated password.

If I do a replacement in my sed line with

's/["/]/!/g'

the script fails to execute. However I tested this from command line like

echo "this /is "a test" | sed -e 's/["/]/!/g'

Then I get the right result : this !is !a !test

If I eliminate this replacement section from my sed line, php script does execute properly. What seems to be the issue? I thought characters within brackets [] is safe and does not need escaping.. is that the issue?

Advertisement

Answer

In PHP, if you use the command in a double quoted string literal, you need to escape the " with a single backslash, ", and the backslash inside the bracket expression must also be escaped.

In the end, this must look like

sed 's/["/\]/!/g'
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement