我可以json_encode一个PHP数组并获取值作为JS RegExp对象?

我有一些正则表达式模式存储为PHP数组中的字符串。 我需要它们作为JavaScript RegExp对象输出。 有没有办法通过使用PHP的 json_encode </ code>来做到这一点?</ p>

示例:</ p>

  $ array = [
'regex'=&gt; '/ some-regex /'
];
return json_encode($ array);
</ code> </ pre>

所需的输出:</ p>

  {“regex”:/ some-regex /} 
</ code> </ pre>

当前输出:</ p>

  {“regex”:“/ some-regex /”} 
</ code> </ pre>
</ div>

展开原文

原文

I have some regex patterns stored as strings inside a PHP array. I need them to be outputted as JavaScript RegExp objects. Is there any way to do this by using PHP's json_encode?

Example:

$array = [
   'regex' => '/some-regex/' 
];
return json_encode( $array );

Desired output:

{"regex": /some-regex/}

Current output:

{"regex": "/some-regex/"}

dotwc62080
dotwc62080 toolong
一年多之前 回复
dortmundbvb0624
dortmundbvb0624 对-哦,我做了第一个评论:-)。继续关于使用新的Regexp()是没有必要的。
一年多之前 回复
douzhi9939
douzhi9939 -恕我直言,第一和第二评论非常清楚地回答。
一年多之前 回复
douke6857
douke6857 它只是我,还是我们都错过了这个问题:有没有办法通过使用PHP的json_encode来做到这一点?答案是不。
一年多之前 回复
doushang2021
doushang2021 一旦得到{“regex”:“/some-regex/”},只需使用新的RegExp()构造函数循环它们,该构造函数接受表示正则表达式的字符串。
一年多之前 回复
dongqiao9583
dongqiao9583 (yikes:-)-从OPs术语派生,一旦从JSON转换为JavaScript对象,有{regex:newRegExp(/some/)}是有效的。换句话说,将RegExp对象作为对象属性。
一年多之前 回复
dpf56454
dpf56454 即使它是一个字符串,你仍然可以通过在RegExp的构造函数中传递它来使用它。示例:letpattern=newRegExp(theRegexString)然后使用模式而不是正则表达式。
一年多之前 回复
douzhang8033
douzhang8033 {“regex”:/some-regex/}是无效的JSON。
一年多之前 回复
doulvli9462
doulvli9462 你不能使用PHP创建JavaScript数据结构,没有。
一年多之前 回复

2个回答



不,你不能。 因为您正在通过刚刚包装数据的JSON结构。 json标准的字符串以“并以...结尾”开头,否则它是一个数字或数组的istance。</ p>

我想知道它有类似的事情。 我想根据结果为JS创建一个函数,但在服务器端使用php。 通常,当您遇到此问题时,这意味着您需要重新考虑一下页面/应用程序的结构。</ p>

但正如他们在您的问题的评论中指出的那样,您可能会找到变通方法 在HTTP请求结束后在本地构建对象。</ p>

还建议阅读:安全性问题JSON与eval </ p>
</ div>

展开原文

原文

No, you can't. Because you're passing through JSON structure which just wraps-data. A string by json standard starts with " and ends with ", otherwise it's a number or an array for istance.

I wondered a similiar thing about it. I wanted to create a function for JS but on server side with php, based on the result. Usually when you face this problem it means you've to rethink a bit the structure of your page/app.

However as they pointed out in the comments to your question, you might find workarounds to build the object locally once the HTTP request is ended.

Also would recommend reading this: Security issues JSON with eval

Not directly with json_encode. All PHP does is create arrays and objects containing simple scalar types. It cannot create JS objects like RegExp or call JS functions directly. If all you're going to pass is that sample data in the question you can always do

$array = [
   'regex' => '/some-regex/' 
];
$js_string = '';
foreach($array as $key => $value) $js_string .= '"' . $key . '": new RegExp("' . $value ' "),';
return '{' . $js_string . '}';

That's really clunky code and, of course, it falls apart if $array contains anything but regular expressions (and it assumes that everything in there is safe for JS). But if that's all you have to do...

Wait! Is there a better way?

Indeed there is. The better thing to do is have json_encode do it's thing (it's kinda made to handle JSON) and then have your JS create the objects itself

let regex_list = <?php echo json_encode($array) ?>;
let myregex = {};
regex_list.forEach(function(regex, index) {
    myregex[index] = new RegExp(regex);
});

This is a much cleaner solution. We don't have to worry about the data coming from PHP, so JS can take it from there and build a JS object that holds all the RegExp objects you want to create

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐