用另一个引号(')str_replace替换单引号(')

Ale*_*eza -3 php string replace htmlspecialchars str-replace

我试图'用另一个quote()替换单引号(),但我似乎无法得到任何工作.另外,如何在多个字符串上进行此操作($text, $title, $notice)

输入:不要

输出:不要

我在尝试这个:

$text = str_replace(array("'",'"'), array('’'), $text);    
$text = htmlentities(str_replace(array('"', "'"), '’', $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $_POST['text']));
$text = str_replace("'" ,"’",$text);
$text = str_replace("'" ,"’",$text);
$text = str_replace(array("'"), "’", $text);
$text = str_replace(array("\'", "'", """), "’", htmlspecialchars($text));
$text = str_replace(array('\'', '"'), '’', $text);
$text = str_replace(chr(39), chr(146), $text);
$text  = str_replace("'", "&ampquot;", $text); 
Run Code Online (Sandbox Code Playgroud)

这些都不起作用.

Qua*_*one 5

当您使用阵列作为替换时,请提供与针一样多的替换.否则使用简单的字符串.

<?php
$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), '’', $text);  
echo $text;

$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), array('’', '`'), $text);  
echo $text;
?>
Run Code Online (Sandbox Code Playgroud)

要对多个变量执行该任务,您可以执行此操作

<?php
$text   = 'Peter\'s cat\'s name is "Tom".';
$title  = 'Peter\'s cat\'s name is "Tom".';
$notice = 'Peter\'s cat\'s name is "Tom".';

foreach([&$text, &$title, &$notice] as &$item)
  $item = str_replace(array("'",'"'), '’', $item);  

echo "text: $text<br>title: $title<br>notice: $notice";
?>
Run Code Online (Sandbox Code Playgroud)