Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- subject_prefix = 'This String From User Input'
- msg_footer = """This one too."""
- echo "subject_prefix = '".$subject_prefix."'n";
- <?php
- function py_escape($input) {
- $descriptorspec = array(
- 0 => array("pipe", "r"),
- 1 => array("pipe", "w")
- );
- $process = proc_open(
- "python -c 'import sys; sys.stdout.write(repr(sys.stdin.read()))'",
- $descriptorspec, $pipes);
- fwrite($pipes[0], $input);
- fclose($pipes[0]);
- $chunk_size = 8192;
- $escaped = fread($pipes[1], $chunk_size);
- if (strlen($escaped) == $chunk_size) {
- // This is important for security.
- die("That string's too big.n");
- }
- proc_close($process);
- return $escaped;
- }
- // Example usage:
- $x = "string rfull noft crappy stuff";
- print py_escape($x);
- function python_string_escape( $string )
- {
- $string = preg_replace( "/\\/", "\\", $string ); # \ (first to avoid string re-escaping)
- $string = preg_replace( "/n/", "\n", $string ); # n
- $string = preg_replace( "/r/", "\r", $string ); # r
- $string = preg_replace( "/t/", "\t", $string ); # t
- $string = preg_replace( "/"/", "\"", $string ); # "
- $string = preg_replace(
- "/([x{00}-x{1F}]|[x{7F}-x{FFFF}])/ue",
- "sprintf("\u%04X",uniord("$1"))",
- $string );
- return $string;
- }
- function uniord($c) {
- $h = ord($c{0});
- if ($h <= 0x7F) {
- return $h;
- } else if ($h < 0xC2) {
- return false;
- } else if ($h <= 0xDF) {
- return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
- } else if ($h <= 0xEF) {
- return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
- | (ord($c{2}) & 0x3F);
- } else if ($h <= 0xF4) {
- return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
- | (ord($c{2}) & 0x3F) << 6
- | (ord($c{3}) & 0x3F);
- } else {
- return false;
- }
- }
- function escape_string($text, $type) {
- // Escape backslashes for all types of strings?
- $text = str_replace('\', '\\', $text);
- switch($type) {
- case 'single':
- $text = str_replace("'", "\'", $text);
- break;
- case 'double':
- $text = str_replace('"', '\"', $text);
- break;
- // etc...
- }
- return $text;
- }
Add Comment
Please, Sign In to add comment