Hi There

I have built a script that generates a file based on the users input, this works well but when they enter characters such as " or ; or similar it mucks up. What I would like to do is take all the bad characters out and generate the file based on what is left over.

I know this is probably a regular expression problem but after looking at things for a while i have drawn a blank except turning spaces into _'s.

any ideas would be much appreciated,

Cheers,
nozom

    If you know you want to replace specifically a semi-colon ( ; ) and not an varied expression, you need not use regular expressions. a simple str-replace will work quite fine (and is recommended)

      think i've got it

      $filename = ereg_replace("[^[:alnum:][:space:]]","", $filename);
      
      $filename = ereg_replace("[[:space:]]","_", $filename);
      

      can't figure how to combine the two however.

      Cheers,
      nozom

        Please explain what you are attempting to do here. In other words, why spaces and what about the other characters you mentioned?

          basically i am generating a filename from user input so

          user inputs the title of the page, as :
          "Guide to fishing/eating with cheese"

          obvoiusly i dont want the " and the /, all i want is the alpha numeric characters and the spaces

          i've used

          $filename = ereg_replace("[^[:alnum:][:space:]]","", $filename); 
          

          to remove all non-alphanumeric characters leaving spaces
          and then

          $filename = ereg_replace("[[:space:]]","_", $filename);
          

          to convert the spaces to underscores.

          that works great (i think) what would be nice is to make it one expression for good housekeeping.

          Cheers,
          nozom

            Didn't try it with eregi_replace but this should work:

            $filename = preg_replace("/[^a-zA-Z0-9]/", "", $
            filename);
            
            $filename = str_replace ( " ", "", $filename);
            
            

            Then again (I haven't tested this) the first replace may actually take care of the spaces too (it should)

              Write a Reply...