Hi,

I'm trying to use preg_replace to:

1) Check if a string contains any characters that are not allowed.
2) Replace any not allowed characters with a blank ''.

I've been using preg_match for a while and it's been fine for doing number 1 from the list above. However, I'm trying to find out if there is a way to reverse my pattern (that checks for unallowed characters in a string) for doing a number 2.

Is there a way to reverse a reg exp pattern by using [] or something?

Here is a sample pattern:/(\w+)(\W[<>]+)$/i

Any help is appreciated.

  • Andy
    bladx wrote:

    Hi,

    I'm trying to use preg_replace to:

    1) Check if a string contains any characters that are not allowed.
    2) Replace any not allowed characters with a blank ''.

    I've been using preg_match for a while and it's been fine for doing number 1 from the list above. However, I'm trying to find out if there is a way to reverse my pattern (that checks for unallowed characters in a string) for doing a number 2.

    Is there a way to reverse a reg exp pattern by using [] or something?

    Here is a sample pattern:/(\w+)(\W[<>]+)$/i

    Any help is appreciated.

    • Andy

    Using ^ matches everything but the characters in your regex. So, if you had something like ABC and had DEF on a line of input, it would match. However, if you have ABCDEF it would not, because you are looking for input without ABC.

      You might need to use str_replace with an array passed as the first argument to do this.

        Write a Reply...