【问题标题】:Removing special Characters from start and end从开头和结尾删除特殊字符
【发布时间】:2014-05-27 16:00:16
【问题描述】:

我想删除字符串开头和结尾的特殊字符。

preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $String);

这会从整个文件中删除特殊字符

我想要的是,如果字符串是 'S-O@ ,它应该返回 SO

有什么帮助吗?

【问题讨论】:

标签: php regex string preg-replace


【解决方案1】:

试试这个:

preg_replace( '/^\W*(.*?)\W*$/', '$1', $string )

/* -----------------------------------------------------------------------------------
  ^                        the beginning of the string
    \W*                    non-word characters (all but a-z, A-Z, 0- 9, _) (0 or more times (matching the most amount possible))
    (                      group and capture to \1:
      .*?                  any character except \n (0 or more times(matching the least amount possible))
    )                      end of \1
    \W*                    non-word characters (all but a-z, A-Z, 0-9, _) (0 or more times (matching the most amount possible))
  $                        before an optional \n, and the end of the string
------------------------------------------------------------------------------------- */

【讨论】:

    【解决方案2】:

    PHP 的trim 函数可能会对您有所帮助,它使用第二个参数传递您想要删除的字符。问题是您必须列出要删除的字符,而不是要保留的字符。

    trim($string,$remove);
    

    其中 $remove 包含要从开头和结尾删除的字符。

    【讨论】:

      【解决方案3】:

      我们创建了一个函数并检查 javascript 的特殊字符

       var string  = "@@@@@M@@@@U###@@";
       var output = filterSpecialChar(string);
       console.log(output); //M@@@@U
      
       function filterSpecialChar(string){
           var arrayString = string.split("");
           var count = arrayString.length/2;
           var check_float =  Number(count) === count && count % 1 !== 0;
           var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
           if(check_float == true){
              var start_length = parseInt(count)+1;
              var end_length = parseInt(count);
           }else{
              var start_length = parseInt(count);
              var end_length = parseInt(count);
           }
      
              for(var i=0;i<parseInt(arrayString.length);i++){
      
              if(format.test(arrayString[i])){
                arrayString[i] = null; 
              }else{
                  break;
              }   
          }
      
          for(var i=arrayString.length-1;i>0;i--){
              if(format.test(arrayString[i])){
                arrayString[i] = null; 
              }else{
                  break;
              }   
          }
      
         var arrayString = arrayString.filter(function(data){return data != null});
         return arrayString.join('');
       }  
      

      【讨论】:

        【解决方案4】:

        尝试使用锚点:

        ^[^a-zA-Z0-9_ %\[\]\.\(\)%&-]|[^a-zA-Z0-9_ %\[\]\.\(\)%&-]$
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-04
          • 2020-04-16
          • 1970-01-01
          • 2013-01-12
          • 2011-12-22
          • 1970-01-01
          相关资源
          最近更新 更多