I'm working on removing extra spaces between keyword the special characters below.

"~", "`", "\"" , "\" , "!", "@" , "$"

I did a filter on certain of them.

$filter = array("~", "`", "\"" , "\\" , "!", "@" , "$" , "%" , "^" , "&" , "*" , "(", ")" , "+" , "{" , "}" , "[" , "]" ,  "<" , ">" ,  "?" , "/" , "|" , "=");

The thing is when user does a search on "1mm Crack (&)",
the special characters get trimmed, leaving a extra spacing after the Keywords "1mm Crackspace"
So it is like "1mm Crack (&)" to "1mm Crack "
Is there any way i can trim the searched keywords to just "1mm Crack" if special characters were key in..

So i was thinking if it's possible to do a removing of extra whites spaces between the keyword and the special characters

Something like

$filter = array("~", "`", "\"" , "\\" , "!", "@" , "$" , "%" , "^" , "&" , "*" , "(", ")" , "+" , "{" , "}" , "[" , "]" ,  "<" , ">" ,  "?" , "/" , "|" , "=");
if(!empty($filter) { 
$trimmedKeyword = str_replace($filter, '', $trimmedKeyword);
} // end if

    Hope i did it right..

    $filter = array("\"" , "\\" , "!", "@" , "$" , "%" , "^" , "&" , "*" , "(", ")" , "+" , "{" , "}" , "[" , "]" ,  "<" , ">" ,  "?" , "/" , "|" , "=");
    $trimmedKeyword = str_replace($filter, '', $trimmedKeyword);
    if (!empty($filter)){
    $trimmedKeyword = preg_replace('#\s+#','',$trimmedKeyword);
    }
    

      but now when i do a search on "1mm crack", it trims the white space between the 1mm and crack..

      This is to say "1mmcrack"

      how should i keep the white spaces and only trim white spaces between the $filter array special characters

      $Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
      $trimmedKeyword = trim($Keyword);
      $trimmedKeyword = stripslashes($trimmedKeyword);
      $filter = array("\"" , "\\" , "!", "@" , "$" , "%" , "^" , "&" , "*" , "(", ")" , "+" , "{" , "}" , "[" , "]" ,  "<" , ">" ,  "?" , "/" , "|" , "=");
      $trimmedKeyword = str_replace($filter, '', $trimmedKeyword);
      if (!empty($filter)){
      $trimmedKeyword = preg_replace('#\s+#','',$trimmedKeyword);
      }
      $trimmedKeyword = str_replace('"', '', $trimmedKeyword);
      //trim whitespace from the stored variable
      $trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword);
      $trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
      

        Just try something like this:

        $Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
        $trimmedKeyword = preg_replace('/ (?= )/', '', trim(preg_replace('/[\\~`!@$%^&*()+{}[\]<>?\/|=]/', '', trim($Keyword))));
        

        EDIT: I forgot to take out extra spaces in the middle. Now, that should do it for you. This works for me (I have PHP v4.3.11). The outside preg_replace should be '/ (?= )/'.

          Thank i tried doing it your way

          $Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
          $trimmedKeyword = trim($Keyword);
          $trimmedKeyword = trim(preg_replace('/[\\\]~`!@$%^&*()+{}[<>?\/|=]/', '', $Keyword));
          

          I returned an error statement:

          Got error 'repetition-operator operand invalid' from regexp

            I edited my previous post. Try that. I get no errors and stuff is removed including any extra spaces between words.

            Good luck.

            🙂

              I tried to do something like this But it is still not working ..

              $filter = array("\"" , "\\" , "!", "@" , "$" , "%" , "^" , "&" , "*" , "(", ")" , "+" , "{" , "}" , "[" , "]" ,  "<" , ">" ,  "?" , "/" , "|" , "=");
              $trimmedKeyword = str_replace($filter, '', $trimmedKeyword);
              
              if (isset($filter)){
              	$trimmedKeyword = preg_replace('#\s+#','',$trimmedKeyword);
              }
              $trimmedKeyword = str_replace('"', '', $trimmedKeyword);
              
              
              if (empty($filter)){
              	$trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword);
              }	
              $trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
              

              I searched "1mm Crack" but the keywords were joined together "1mmCrack"..
              I only want to trim the white spaces when $filter array special charcters were used

                Your last post where you check isset($filter) is always going to be true and it's not going to do what you want. It will replace ALL spaces between words, which is not what you want.

                Use what I last gave you. It works. Here it is again. I included to strip double quotes too, and handled removing the spaces if there is a period or comma after the special characters stripped:

                $Keyword = (isset($GET['Keyword'])) ? $GET['Keyword'] : '';
                $trimmedKeyword = preg_replace('/ (?=[ .,])/', '', trim(preg_replace('/[\~`!@$%&*()+{}[]<>?\/|="]/', '', trim($Keyword))));
                $trimmed_arrayKeyword = explode(' ', $trimmedKeyword);

                Just try it (copy and paste it into your code, don't try and write it out). Note that this will remove extra spaces between words no matter what - i.e. [pre]'1mm Crack'[/pre] will become '1mm Crack'.

                Another example:[pre]'I make lots of dollars ($), which is nice!.'[/pre] will become 'I make lots of dollars, which is nice.'

                🙂

                  Write a Reply...