ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP中更有效的字符串清理正则表达式

2019-10-31 17:32:36  阅读:248  来源: 互联网

标签:code-cleanup clean-urls string php regex


好吧,我希望有人可以帮我一点正则表达式.

我正在尝试清理字符串.

基本上,我是:

>将所有字符替换为A-Za-z0-9除外.
>用单个替换实例替换替换的连续重复副本.
>从字符串的开头和结尾修剪替换.

输入示例:

(&&(%()$()#&#&%& %%(%$-_狗跳过日志*(&)$%&)#)@#%& )& ^)@#)

要求的输出:

狗跳过了原木

我目前正在使用此非常分散的代码,并且只知道有一种更优雅的方法可以完成此任务.

function clean($string, $replace){

    $ok = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    $ok .= $replace;
    $pattern = "/[^".preg_quote($ok, "/")."]/";

    return trim(preg_replace('/'.preg_quote($replace.$replace).'+/', $replace, preg_replace($pattern, $replace, $string)),$replace);
}

Regex-Fu Master能否请我提供一个更简单/更有效的解决方案?

BotondBalázs和hakre提出并解释了一个更好的解决方案:

function clean($string, $replace, $skip=""){
    // Escape $skip
    $escaped = preg_quote($replace.$skip, "/");

    // Regex pattern
    // Replace all consecutive occurrences of "Not OK" 
    // characters with the replacement
    $pattern = '/[^A-Za-z0-9'.$escaped.']+/';

    // Execute the regex
    $result = preg_replace($pattern, $replace, $string);

    // Trim and return the result
    return trim($result, $replace);
}

解决方法:

我不是“正则表达式忍者”,但我将按照以下方式进行操作.

function clean($string, $replace){
    /// Remove all "not OK" characters from the beginning and the end:
    $result = preg_replace('/^[^A-Za-z0-9]+/', '', $string);
    $result = preg_replace('/[^A-Za-z0-9]+$/', '', $result);

    // Replace all consecutive occurrences of "not OK" 
    // characters with the replacement:
    $result = preg_replace('/[^A-Za-z0-9]+/', $replace, $result);

    return $result;
}

我想这可以进一步简化,但是在处理正则表达式时,清晰度和可读性通常比聪明或编写超最佳代码更重要.

让我们看看它是如何工作的:

> / ^ [^ A-Za-z0-9] /:

> ^匹配字符串的开头.
> [^ A-Za-z0-9]与所有非字母数字字符匹配
>表示“匹配一项或多项先前的内容”

> / [^ A-Za-z0-9] $/:

>与上述相同,但$匹配字符串的结尾

> / [^ A-Za-z0-9] /:

>与上述相同,但它也匹配中弦

编辑:OP是正确的,可以用对trim()的调用来替换前两个:

function clean($string, $replace){
    // Replace all consecutive occurrences of "not OK" 
    // characters with the replacement:
    $result = preg_replace('/[^A-Za-z0-9]+/', $replace, $result);

    return trim($result, $replace);
}

标签:code-cleanup,clean-urls,string,php,regex
来源: https://codeday.me/bug/20191031/1977197.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有