Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Created September 11, 2014 15:38
Show Gist options
  • Save khoand0000/5f94cd40bb9d0a888931 to your computer and use it in GitHub Desktop.
Save khoand0000/5f94cd40bb9d0a888931 to your computer and use it in GitHub Desktop.
http://phpsnips.com/603/Escape-user-input-before-preg_replace#.VBG_A_mSySo Sometimes you need to do preg_replace on user input. Don't forget to escape all special characters or you'll get funny results.
<?php
$input = str_replace(array('^', '.', '|', '(', ')', '[', ']', '*', '+', '?', '{', '}', '$' ),
array('\^', '\.', '\|', '\(', '\)', '\[', '\]', '\*', '\+', '\?', '\{', '\}', '\$' ), $input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment