[英]Replace a single quote ( ' ) with another quote (’) str_replace
I am trying to replace a single quote ( '
) with another quote ( '
), but I can't seem to get anything to work.我试图用另一个引号 ( '
) 替换单引号 ( '
),但我似乎无法得到任何工作。 Also, how can I make this work on multiple strings ($text, $title, $notice)
?另外,如何在多个字符串($text, $title, $notice)
上进行这项工作?
input: don't输入:不要
output: don't输出:不要
I am trying this:我正在尝试这个:
$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("'", """, $text);
None of this works.这些都不起作用。
When you use an array as replacements, give as many replacements as you give needles.当您使用数组作为替代品时,请提供与针头一样多的替代品。 Else use a simple string.否则使用简单的字符串。
<?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;
?>
To perform that task on multiple variables you could do要对多个变量执行该任务,您可以这样做
<?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";
?>
I tried using preg_replace()
and it worked perfectly first time:我尝试使用preg_replace()
并且第一次运行得很好:
$text = "Someone told me about a 'bacon and cheese sandwich'";
$text = preg_replace("#\'#", '’', $text);
echo $text;
Output输出Someone told me about a 'bacon and cheese sandwich'
Give that a go.试一试吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.