string preg_quote ( string $str [, string $delimiter ] )

str에서 정규 표현식 문법(. \ + * ? [ ^ ] $ ( ) { } = ! < > | :)에 포함되는 모든 문자의 앞에 백슬래시를 붙여 반환하고, delimiter를 정의하면, 그 문자들도 이스케이프합니다. PCRE 함수에서 요구하는 구분자를 이스케이프 할 때 유용합니다. /는 가장 널리 사용되는 구분자입니다.

<?php
 $keywords = '$40 for a g3/400';
 $keywords = preg_quote($keywords, '/');
 echo $keywords; // \$40 for a g3\/400
 ?>

<?php
 $textbody = "This book is *very* difficult to find.";
 $word = "*very*";
 $textbody = preg_replace("/" . preg_quote($word) . "/", "<i>" . $word . "</i>", $textbody);
 echo $textbody; // This book is *very* difficult to find.
 ?>

<?php
 $text = 'Test: *#5*';
 $search = "*#5*";
 $replace = "*$5\\1*";
 $search = preg_quote($search, "/");
 $replace = str_replace (array('\\','$'),array('\\\\','\$'),$replace);
 $new = preg_replace("/$search/", "$replace", $text);
 echo "Input: $text"; // 결과: Test: *#5*

 echo "Output: $new"; // 결과: Test: *$5\1*
 ?>

<?php
 // $_GET['url'] => alert(document.cookie)
 $href = $_GET['url'];

 echo "<a onmouseover='" . $href . "'>xxs link</a>";

 // 결과: alert(document.cookie)
 echo "<IFRAME SRC=# onmouseover='" . preg_quote($href) . "'></IFRAME>";

 // 결과: alert\(document.cookie\) 
 ?>

0 댓글