【问题标题】:PhP regex, remove white spaces and all special characters from string [duplicate]PhP正则表达式,从字符串中删除空格和所有特殊字符[重复]
【发布时间】:2017-12-11 20:07:12
【问题描述】:

我有一个变量 $name,它有时可能包含空格和特殊字符,如 '%' '&' 等。如何使用正则表达式或任何其他方式删除所有这些?

 */
public function handle()
{
    $urls = Business::pluck('ical');
    $names = Business::pluck('name');
    foreach ($urls as $url) {
        foreach ($names as $name) {
            $test= explode("\n", $name);
            dd($test);
        $response = Curl::to($url)
            ->download('ical/'.$name.'.ics');
    }   

【问题讨论】:

    标签: php regex laravel laravel-5


    【解决方案1】:

    使用 PHP 的 preg_replace() 函数。

    $name = preg_repalce('/[^\w\d]+/', '', $name);
    

    正则表达式/[^\w\d]+/ 匹配所有空格和特殊字符。

    https://www.tinywebhut.com/regex/2

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      相关资源
      最近更新 更多