Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 引号、preg_替换或str_替换的问题_Php_Preg Replace_Str Replace - Fatal编程技术网

Php 引号、preg_替换或str_替换的问题

Php 引号、preg_替换或str_替换的问题,php,preg-replace,str-replace,Php,Preg Replace,Str Replace,好吧,我以前用过正则表达式,但这次我的大脑似乎不想和我一起工作 我正在尝试从字符串中的一些json中删除一些“和一些” $string = '"cusComment": "Direct from user input, so need to remove "double quotation marks" and \'single ones as well", "intComment": "" }}'; $blab = preg_replace('/["cusComment": "]"[",

好吧,我以前用过正则表达式,但这次我的大脑似乎不想和我一起工作

我正在尝试从字符串中的一些json中删除一些“和一些”

$string  = '"cusComment": "Direct from user input, so need to remove "double quotation marks" and \'single ones as well", "intComment": "" }}';

$blab = preg_replace('/["cusComment": "]"[", "intComment"]/', "", $string);

echo $blab;
这几乎适用于“删除”,但会产生一些不必要的结果

编辑:
我想你可以用“另一种方式”,只让字母、数字、点字、逗号、破折号、下划线和空格通过。。。仍然需要帮助:)

这可以通过
(*跳过)(*失败)
技术实现。它可以有效地取消特定匹配的资格,并且只返回所需的子字符串(在本例中为双引号)

下面的模式只处理与regex的复杂双引号匹配。一个简单的调用
str_replace()
将处理单引号,因为它们都将被省略

模式:
/(“[:,]”“| ^“|”\}{2})(*跳过)(*失败)\\'/

此模式表示,取消以下比赛的资格:

  • 由冒号或逗号和空格分隔的两个双引号
  • 行开头的双引号
  • 一个双引号,后跟一个空格和两个右括号
  • 前面有斜杠的单引号

PHP代码:()*该模式在PHP实现中需要额外的斜杠

$string  = '"cusComment": "Direct from user input, so need to remove "double quotation marks" and \'single ones as well", "intComment": "" }}';
$blab = preg_replace('/("[:,] "|^"|" \}{2})(*SKIP)(*FAIL)|"|\\\'/', "", $string);
echo $blab;
我不确定您对这个字符串做了哪些进一步的修改来准备它,但是这些任务可能与这个模式结合得很好,也可能与这个模式结合得不好


如果要删除尾随的
}
和空空格,那么调用
rtrim($blab,'}')
似乎是合乎逻辑的,但也可以省去额外的函数调用,只扩展模式:
/(“[:,]”“| ^”|“(?={2}$)(*SKIP)(*FAIL){124\\\\\\\\\\\\\\\\\\{2}$/
我想你不明白什么是
[]
表示在regexp中。它用于匹配一组字符,但您正在使用它进行某种分组。为什么首先需要删除引号
json_encode()
正确地转义它们,它们不应该引起问题。@Barmar我不做json_encode这是html电子邮件中的一些“json”,它是按原样提供的,我们无法更改这个旧的奇怪系统。接下来我会做:
$obj=json\u decode(utf8\u decode($message\u nohtml\u utf8),true);foreach($obj as$key=>$entry){…..