2016-09-30 2 views
-4

문자열 사용자 입력에 변수을 바꿉니다. 문자열에 동의 (')하는 방법을 알 수 있습니까? 예를 들어PHP - 배열의 문자열 바꾸기

$string = ' *this* is 'a' test' '; 
$regexes = array('/~(.*?)~/six','/\*(.*?)\*/six'); 
$replaces = array('<i>$1</i>','<b>$1</b>'); 
$new_string = preg_replace($regexes, $replaces, $string); 

echo $new_string; 

말을하자 나는 굵은기울임 텍스트로 변경 할 수 있지만, 문자열은 '' ''경우, 에러를 줄 것이다. 이것을 어떻게 성취하고 싶습니까?

+0

http://parsedown.org/이 - 바퀴를 재발견하지 마십시오! –

+1

'addslashes ($ string)'를 사용하십시오; –

+0

너희들이 의미하는 것은 : preg_quote() 나는 추정한다)) – Deep

답변

1

이 내용은 도움이 될 수 있으니 addslashesstripslashes을 필요에 따라 사용하십시오.

<?php 
$string = " *this* is 'a' test' "; 
$string = addslashes($string); 
$regexes = array('/~(.*?)~/six','/\*(.*?)\*/six'); 
$replaces = array('<i>$1</i>','<b>$1</b>'); 
$new_string = preg_replace($regexes, $replaces, $string); 
echo stripslashes($new_string); 
?> 

이 출력 : '는'테스트 '

+1

도움에 감사드립니다! 그것은 작동합니다. – Amran