0

Hello, I am trying to make a array in php that looks like this:

$array= array("test"2"", "'test"2"'");

Of course this doesn't work because of the two strings, but is there a way in php to get is to ignore all quotes in such a string so it sees it as a valid string? The problem being that I need the quotes in my string and it are double quotes and single quotes.

EDIT: Thanks for the quick response! I was wondering if there is a way of doing this with code? I have a huge string to do this with?

$array= array("test\"2\"", "'test\"2\"'");
1
  • If you need to perform this escaping programmatically, where are you pulling these values from? If you are writing them by hand, then you you should be manually escaping them. If they are coming from somewhere else, they are probably already escaped or you would have an error. Commented Dec 7, 2017 at 14:41

3 Answers 3

3

You have to escape quotes in strings.

$array= array("test\"2\"", "'test\"2\"'");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick response! I was wondering if there is a way of doing this with code? I have a huge string to do this with?
Post this in your question
1

If you write text in double quotes, you have to escape it inside text using \.

$array = array("test\"2\"", "'test\"2\"'");

If you write text in single quotes, you have to escape single quotes same way

$array = array('test"2"', '\'test"2"\'');

Comments

1

If your array $array= array("test\"2\"", "'test\"2\"'"); is build using a loop then simply use PHP function addslashes() that will escape the redundant qotations.

$strings = array('abc"def', "efg'hij");

foreach($strings as $str){
  echo  addslashes($str) .PHP_EOL;
}

https://eval.in/914320

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.