如果在php中字符串的0和最后一个索引处可用,如何删除卷曲引号?
如果在php中字符串的0和最后一个索引处可用,如何删除$checkChar值 条件不适用于卷引号 我正在尝试这个如果在php中字符串的0和最后一个索引处可用,如何删除卷曲引号?,php,drupal-8,Php,Drupal 8,如果在php中字符串的0和最后一个索引处可用,如何删除$checkChar值 条件不适用于卷引号 我正在尝试这个 $checkArr = ['"', '“', '”']; $str['quote'] = "“This is a sample text.”"; foreach ($checkArr as $key){ if($str['quote'][0] === $key || $str['quote'][strlen($str['quote']) - 1] === $key {
$checkArr = ['"', '“', '”'];
$str['quote'] = "“This is a sample text.”";
foreach ($checkArr as $key){
if($str['quote'][0] === $key || $str['quote'][strlen($str['quote']) - 1] === $key {
$str['quote'] = str_replace($key, '', $str['quote']);
}
}
您可以使用删除字符串外部的引号:
$checkArr = ['"', '“', '”'];
$str['quote'] = "“This is a sample text.”";
echo trim($str['quote'], implode('', $checkArr));
输出:
This is a sample text.
This is a sample text.
注意
如果字符串外部有多个引号,它们都将被删除,例如
$checkArr = ['"', '“', '”'];
$str['quote'] = "““This is a sample text.””";
echo trim($str['quote'], implode('', $checkArr));
输出:
This is a sample text.
This is a sample text.
您可以使用删除字符串外部的引号:
$checkArr = ['"', '“', '”'];
$str['quote'] = "“This is a sample text.”";
echo trim($str['quote'], implode('', $checkArr));
输出:
This is a sample text.
This is a sample text.
注意
如果字符串外部有多个引号,它们都将被删除,例如
$checkArr = ['"', '“', '”'];
$str['quote'] = "““This is a sample text.””";
echo trim($str['quote'], implode('', $checkArr));
输出:
This is a sample text.
This is a sample text.
ltrim
和rtrim
是否合适?ltrim
和rtrim
是否合适?