2016-09-30 112 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); 
?> 

此輸出:是 'A' 測試」

+1

感謝您的幫助!有用 – Amran