我希望搜索和替换值包含撇号。如何编辑数组以允许这个?
非常赞赏。
function replacecategories($value) {
//here are predefined values
$predefined = array(
array(
'search'=>'Here's an example',
'replaceWith'=>'There's an example'
)
);
//search and replace
foreach ($predefined as $item) {
$search = array_map('trim', explode(',', $item['search']));
$value = str_replace($search, $item['replaceWith'], $value);
}
return $value;
}
答案 0 :(得分:1)
如果我理解你的正确,你需要做的就是,在你的例子中添加一个反斜杠来逃避撇号,如下所示,或者用双引号重新定义变量(“Here's ...”):
function replacecategories($value) {
here are predefined values
$predefined = array(
array(
'search'=>'Here\'s an example',
'replaceWith'=>'There\'s an example'
)
);
//search and replace
foreach ($predefined as $item) {
$search = array_map('trim', explode(',', $item['search']));
$value = str_replace($search, $item['replaceWith'], $value);
}
return $value;
}
答案 1 :(得分:0)
array(
'search'=>"Here's an example",
'replaceWith'=>"There's an example"
)
答案 2 :(得分:0)
撇号在任何语言中都有特殊含义。为了使它实际写出撇号,你需要使用反斜杠 test = [{'a': j[i]['a'], 'token':j[i]['token']} for i in len(json)-1]
来转义特殊字符。
或用双引号封装字符串。