Hi
currently I'm using this bit of code:

$bc= array("#",";","[",".");
$ch= preg_quote(implode('', $bc));
$test=(preg_match('/['.$ch.']/', $name)

This seems to work OK.

But I have 2 slight issues.

  1. how would I check $name for all characters as above but allow one value from the array $bc to be present at the end of $name ?

eg: $name = test# normally this would match the above. How would I stop it matching the # as the last character ? ( and only # at the end )

  1. how would I add the following characters to the array / \ * when I've tried they break the preg_match ?

Thanks for your time and help 🙂

    I might invert the logic of the test, so that if it returns true, this would be OK:

    $test = preg_match('/^[^'.$ch.']+['.$ch.']?$/', $name);
    

      Thanks for the reply.

      Not sure what that does or how it helps ?
      Please can you explain ?

      Thanks

        It uses ^ and $ at the start and end to match against your entire string, rather than match only on a specific character, then it tries to match at least one character that is not in your 'do not use' list, followed by zero or one of the 'do not match' characters.

        So, things like 'test', and 'test#' would be ok, but '#test' and 'test##' would not.

          Ashley Sheridan;11002823 wrote:

          He

          Oops...maybe confusing you with another Ashley? Or maybe I just jumped to a wrong conclusion in the first place? :o

          If it's any consolation, Laserlight's avatar led me similarly astray for quite awhile. 🆒

            Thanks all that makes sense... BUT

            How do I limit this to only one character from my array ?

            Only # at the end, not ,.; etc ??

            Thanks

              Then just use that single character instead of the bracketed character class.

                NogDog;11002827 wrote:

                Oops...maybe confusing you with another Ashley? Or maybe I just jumped to a wrong conclusion in the first place? :o

                If it's any consolation, Laserlight's avatar led me similarly astray for quite awhile. 🆒

                It happens a lot, it's the spelling of the name, in many countries it's spelt the way I spell it for both males and females!

                  NogDog;11002830 wrote:

                  Then just use that single character instead of the bracketed character class.

                  $test = preg_match('/['.$ch.']+['#']?$/', $name);
                  would that be correct ?

                  How do I do it for a fullstop ?
                  $test = preg_match('/['.$ch.']+['.']?$/', $name); ??

                  Thanks

                    Just escape it with a backslash:

                    '/^[^'.$ch.']+\.?$/'
                    

                      Thanks again for your advice 🙂

                      Last question (hopefully)

                      how would I check $name for any character from my array, but ignore if the last character is a . ??

                      So if
                      $name = 1234.5678 this would be bad
                      BUT
                      $name = 12345678. would be OK ?

                      Thanks again 🙂

                        Please ignore my last post.
                        I have that already :p

                        What I mean was, how could I change it to ignore one value from the array ?
                        ( I don't want to delete the value from the array as it is being used elsewhere as well )

                        eg: ignore . so the following would all be OK.

                        $name = 12.23.4.5.6
                        $name = 12.234.34.

                        etc,

                        but
                        $name = 23;342;43 etc.. would still be bad.

                          Write a Reply...