Using preg_replace pattern to clean a string

Windkin

I want to use php preg_replace to remove (note that i want them removed, so i dont want to use htmlentities)

I want to allow the charecters a-z, A-Z, 0-9, and the special chars "@_." only

Amal Murali

Pretty straight-forward:

$string = preg_replace('/[^a-zA-Z0-9@.]+/', '', $string);

In simple English: Replace one or more occurrences of anything that's not an alphabet, number or the symbol @, ., with nothing.

Autopsy:

  • / - starting delimiter
  • [^a-zA-Z0-9@.] - a character class
    • ^ - used to negate the character class
    • a-zA-Z0-9 - alphabets or numbers
    • @ - literal character @
    • . - literal character .
  • + - match the previous quantity one or more times
  • / ending delimiter

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to preg_replace pattern on string

From Dev

Converting a string using preg_replace

From Dev

Using preg_replace to "update" searched string

From Dev

Remove  from string using preg_replace

From Dev

Using preg_replace to transform URLs in a string

From Dev

preg_replace The /e modifier is deprecated using arrays as pattern and replacement

From Dev

How to check whether pattern is matched by using preg_replace only

From Dev

Replace all the first character of words in a string using preg_replace()

From Dev

Converting urls to links in a string using preg_replace()

From Dev

Using preg_replace() to truncate a string AFTER a .com or .net or .org

From Java

Replace a string that matches a pattern using preg_replace_callback

From Dev

preg_replace with regex pattern

From Dev

preg_replace pattern for textarea

From Dev

Preg_replace Pattern Variable

From Dev

preg_replace with regex pattern

From Dev

Reset position pointer to replace all occurrences of a pattern until delimiter in single step using preg_replace

From Dev

Laravel 5.1 - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

laravel 5.2: ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

Replace two values in a preg_replace pattern

From Dev

preg_replace and remainder of string

From Dev

Replace an upper-case string with lower-case using preg_replace() and regex

From Dev

Using preg_replace on an array

From Dev

Issue in using Preg_replace

From Dev

Replace A Pattern In String Using Java

From Dev

preg_replace for particular pattern if not exist

From Dev

preg_replace use matched pattern in replacement

From Dev

PHP preg_replace URL pattern

Related Related

  1. 1

    How to preg_replace pattern on string

  2. 2

    Converting a string using preg_replace

  3. 3

    Using preg_replace to "update" searched string

  4. 4

    Remove  from string using preg_replace

  5. 5

    Using preg_replace to transform URLs in a string

  6. 6

    preg_replace The /e modifier is deprecated using arrays as pattern and replacement

  7. 7

    How to check whether pattern is matched by using preg_replace only

  8. 8

    Replace all the first character of words in a string using preg_replace()

  9. 9

    Converting urls to links in a string using preg_replace()

  10. 10

    Using preg_replace() to truncate a string AFTER a .com or .net or .org

  11. 11

    Replace a string that matches a pattern using preg_replace_callback

  12. 12

    preg_replace with regex pattern

  13. 13

    preg_replace pattern for textarea

  14. 14

    Preg_replace Pattern Variable

  15. 15

    preg_replace with regex pattern

  16. 16

    Reset position pointer to replace all occurrences of a pattern until delimiter in single step using preg_replace

  17. 17

    Laravel 5.1 - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  18. 18

    Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  19. 19

    Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  20. 20

    laravel 5.2: ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  21. 21

    Replace two values in a preg_replace pattern

  22. 22

    preg_replace and remainder of string

  23. 23

    Replace an upper-case string with lower-case using preg_replace() and regex

  24. 24

    Using preg_replace on an array

  25. 25

    Issue in using Preg_replace

  26. 26

    Replace A Pattern In String Using Java

  27. 27

    preg_replace for particular pattern if not exist

  28. 28

    preg_replace use matched pattern in replacement

  29. 29

    PHP preg_replace URL pattern

HotTag

Archive