1

How can I replace yes to 1 and no to 0? Example: (NOT WORKING!)

str_replace("yes","1",$data,"no","0",$data);

4 Answers 4

2

The function str_replace() supports array arguments:

var_dump(
  str_replace(array('yes', 'no'), array('1', '0'), $data)
);

strtr() supports an replacement array:

var_dump(
  strtr($data, array('yes' => 1, 'no' => 0))
);
Sign up to request clarification or add additional context in comments.

Comments

1
str_replace("yes", "1", str_replace("no", "0", $data));

Comments

1
$data = str_replace("yes","1",$data);
$data = str_replace("no","0",$data);

Comments

0
$str = 'This is yes, this is no';
$str = preg_replace(array('/yes/i', '/no/i'), array('1', '0'), $str);

echo $str; // This is 1, this is 0

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.