0

I want to remove the special characters from start and end of a String.

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

This removes special characters from whole file

what I want is that if the string is 'S-O@ , it should return SO

Any help?

user3680538
  • 65
  • 1
  • 6
  • 2
    Shouldn't the example result in `S-O`? As the `-` is not at the start or end of the string? – Veger May 27 '14 at 16:08
  • Possible duplicate of [Regex to trim hyphens from start and end of a string](http://stackoverflow.com/questions/703066/regex-to-trim-hyphens-from-start-and-end-of-a-string) – Anonymous Oct 11 '15 at 13:14

4 Answers4

5

Try this:

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
------------------------------------------------------------------------------------- */
Danijel
  • 12,408
  • 5
  • 38
  • 54
2

PHP's trim function might help you, using the second argument to pass the characters you want removed. Problem is that you have to list the characters you want to removed, rather than the ones you want to keep.

trim($string,$remove);

Where $remove contains the characters you want stripped from the start and end.

Dom
  • 2,980
  • 2
  • 28
  • 41
1

We created one function and check special character for 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('');
 }  
Gaurav verma
  • 679
  • 6
  • 12
0

Try using anchors:

^[^a-zA-Z0-9_ %\[\]\.\(\)%&-]|[^a-zA-Z0-9_ %\[\]\.\(\)%&-]$
tenub
  • 3,386
  • 1
  • 16
  • 25