[英]How do I print and store regex (Regular Expressions) in PHP and MYSQL
我需要能夠存儲和回顯正則表達式。 在我的情況下,用戶將正則表達式輸入到表單中,並且需要稍后將確切的字符序列回顯到屏幕上。 問題是回聲改變了字符。
所以例如我試過這個
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
但是當我回應它時..
回聲$正則表達式;
我得到...
((((amount\">\$)(.*?)(?=)
如果我這樣做
$regex = htmlentities($regex);
我得到了這個,它有助於正則表達式的缺失部分,但不是倍數 ((((
((((amount\">\$)(.*?)(?=</strong>
htmlspecialchars 也沒有幫助。
如何讓它完全按照寫入的方式回顯變量? 我需要做什么才能將它們存儲在 MySQL 中並完全按照所寫的方式檢索它們?
編輯 - 針對下面的一些觀察,我添加了更多細節。 這個新示例是在雲中的 PHP 7.1 服務器上完成的,Centos 7 使用 Chrome 呈現。
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
$page_elements_regex[1][0] = $regex;
$page_elements_regex[1][1] = addslashes($regex);
$page_elements_regex[1][2] = htmlspecialchars($regex);
$page_elements_regex[1][3] = htmlentities($regex);
echo "regex " . $page_elements_regex[1][0] . "<BR>";
echo "addslashes " . $page_elements_regex[1][1] . "<BR>";
echo "htmlspecialcharacters " . $page_elements_regex[1][2] . "<BR>";
echo "htmlentities " . $page_elements_regex[1][3] . "<BR>";
結果
regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)
這也是一個很大的線索,如果你脫掉第一個(像這樣
$regex = '?<=amount\">\$)(.*?)(?=</strong>)';
結果去掉了金額的第一個a?! 它是在解釋正則表達式而不是回顯它嗎?
?(((mount\">\$)(.*?)(?=)
我已經解決了,感覺這個答案有點傻。 我的錯。
在我的代碼的其他地方我有這個
$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';
我不知道為什么這會給出結果,而不是直接錯誤,但是一旦刪除它就一切正常。 底線是 htmlspecialcharacters 和 htmlentities 都給出了正確的答案,經驗教訓。 檢查所有代碼,我的錯誤是在使用 arrays 時,將 $regex 定義為數組和變量,而不是我在這里首先想到的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.