dongruan6001
2016-08-16 09:04
浏览 39
已采纳

阻止PHP将反斜杠添加到字符串[关闭]

I have a function that builds a regex based on an array. The problem is that PHP keeps adding backslashes to some of the characters, and it keeps messing up the regex.

Here is my function:

private static $allowedPermissions = [
/*SV*/   
    'user_add',
    'user_edit',
    'user_delete',
    'user_view'];


    $regexrule = '/';

    foreach (self::$allowedPermissions as $allowedPermission) {
        $regexrule .= '\b'.$allowedPermission.'\b';
        if(end(self::$allowedPermissions) !== $allowedPermission) $regexrule .='|';
    }

    $regexrule .= "/";
    return 'regex:'.$regexrule;

It is adding backslashes where I don't expect them:

regex:\/\\buser_add\\b|\\buser_edit\\b|\\buser_delete\\b|\\buser_view\\b|\\bpatient_add\\b|\\bpatient_edit\\b|\\bpatient_delete\\b|\\bpatient_view\\b|\\bmake_per\\b|\\bmake_per_withconfirmation\\b|\\bconfirm_per\\b|\\beval_per\\b|\\beval_per_withconfirmation\\b|\\bconfirm_per_report\\b\/

Backup screenshot of regex

Is there a workaround?

图片转代码服务由CSDN问答提供 功能建议

我有一个基于数组构建正则表达式的函数。 问题是PHP不断向某些字符添加反斜杠,并且它一直搞乱了正则表达式。

这是我的功能:

  private static $ allowedPermissions = [
 / * SV * / 
'user_add',
  n'user_edit',
'user_delete',
'user_view']; 
 
 
 $ regexrule ='/'; 
 
 foreach(self :: $ allowedPermissions as $ allowedPermission){
 $  regexrule。='\ b'。$ allowedPermission。'\ b'; 
 if(end(self :: $ allowedPermissions)!== $ allowedPermission)$ regexrule。='|'; 
} 
 
 $  regexrule。=“/”; 
返回'正则表达式:'。$ regexrule; 
   
 
 

它正在添加反斜杠,我不指望它们:

<代码>正则表达式:\ / \\ buser_add \\ C | \\ buser_edit \\ C | \\ buser_delete \\ C | \\ buser_view \\ C | \\ bpatient_add \\ C | \\ \\ bpatient_edit C | \\ \\ bpatient_delete C | \\ \\ bpatient_view C | \\ \\ bmake_per C | \\ \\ bmake_per_withconfirmation C | \\ \\ bconfirm_per C | \\ \\ beval_per C | \ \ beval_per_withconfirmation \\ b | \\ bconfirm_per_report \\ b \ /

正则表达式的备份截图

是否有解决方法?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlu7680 2016-08-16 13:34
    已采纳

    I found out that returning it in json format was adding the backslashes.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题