-3

Hello I am using the str_replace but in the comparity I am using extra " thus it is not working. how can I use extra " inside the str_replace

$errorAlerts = str_replace("Password found in list of "most common passwords", please choose a more secure password.", "{$record['c_pw']}", $errorsAndAlerts);

Thanks for your input

Emma Linnery
  • 101
  • 9
  • `str_replace('Password found in list of "most common passwords", please choose a more secure password.', "{$record['c_pw']}", $errorsAndAlerts);` ? – Albzi Jul 11 '17 at 09:06
  • 1
    use surrounding single quotes instead of double quotes, or [escape your string](https://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php) – hassan Jul 11 '17 at 09:06
  • 1
    Go learn some basics: http://php.net/manual/en/language.types.string.php – CBroe Jul 11 '17 at 09:09

3 Answers3

3

Either escape

$errorAlerts = str_replace("Password found in list of \"most common passwords\", please choose a more secure password.", $record['c_pw'], $errorsAndAlerts);

or use single quotes

$errorAlerts = str_replace('Password found in list of "most common passwords", please choose a more secure password.', $record['c_pw'], $errorsAndAlerts);

Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
  • Hi yea, While using either the \ or the single quotes I do not get errors but the str_replace does not work aka the comparison does not work. I am using a cms and this is the error it is giving out, but I have a multilingual website thus I wanted to replace it with the correct language but the comparison does not work. if you have any input as to why I would be grateful. – Emma Linnery Jul 11 '17 at 09:59
  • @EmmaLinnery: Btw you have two different variables mentioned in the code: `$errorAlerts` and `$errorsAndAlerts`. Could it be that you are saving the replaced text to the wrong variable? – Jirka Hrazdil Jul 11 '17 at 10:09
  • thank you sorry, $errorsAlerts = str_replace("Password found in list of \"most common passwords\", please choose a more secure password.", "ding dong", $errorsAlerts); It doesnt work. – Emma Linnery Jul 11 '17 at 10:15
1

Us single quotes around a double quoted literal

$errorAlerts = str_replace('Password found in list of "most common passwords", please choose a more secure password.', 
                            $record['c_pw'], 
                            $errorsAndAlerts);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
1

You can use two methods here

Use \ to escape double quotes

$errorAlerts = str_replace("Password found in list of \"most common passwords\", please choose a more secure password.", {$record['c_pw']}, $errorsAndAlerts);

Or you can use singe quotes.

$errorAlerts = str_replace('Password found in list of "most common passwords", please choose a more secure password.', {$record['c_pw']}, $errorsAndAlerts);

To specify a literal double quote, escape it with a backslash ().

Here is a good reference from php page how to use single and double quotes. Check out link

Edison Biba
  • 4,384
  • 3
  • 17
  • 33