简体   繁体   English

如何在 PHP 和 MYSQL 中打印和存储正则表达式(正则表达式)

[英]How do I print and store regex (Regular Expressions) in PHP and MYSQL

I need to be able to store and echo regular expressions.我需要能够存储和回显正则表达式。 In my case the user enters the regex into a form and that exact sequence of characters needs to be echo-ed to the screen sometime later.在我的情况下,用户将正则表达式输入到表单中,并且需要稍后将确切的字符序列回显到屏幕上。 The problem is that the echo changes the characters.问题是回声改变了字符。

So for instance I have tried this所以例如我试过这个

$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';

but when I echo it..但是当我回应它时..

echo $regex;回声$正则表达式;

I get...我得到...

((((amount\">\$)(.*?)(?=)

If I do this如果我这样做

$regex = htmlentities($regex); $regex = htmlentities($regex);

I get this which helped with the missing part of the regex but not the multiple ((((我得到了这个,它有助于正则表达式的缺失部分,但不是倍数 ((((

((((amount\">\$)(.*?)(?=</strong>

htmlspecialchars did not help either. htmlspecialchars 也没有帮助。

How do I get it to echo the variable exactly as it is written?如何让它完全按照写入的方式回显变量? And what would I need to do to store them in MySQL and retrieve them exactly as written?我需要做什么才能将它们存储在 MySQL 中并完全按照所写的方式检索它们?

EDIT - in response to some observations below, I add a bit more detail.编辑 - 针对下面的一些观察,我添加了更多细节。 This new example was done on a PHP 7.1 server in the cloud, Centos 7 rendered using Chrome.这个新示例是在云中的 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>";

Results结果

regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)

It is also a big clue that if you take off the first ( like this这也是一个很大的线索,如果你脱掉第一个(像这样

$regex = '?<=amount\">\$)(.*?)(?=</strong>)';

The result removes the first a of amount?!结果去掉了金额的第一个a?! Is it interpreting the regex instead of echoing it?它是在解释正则表达式而不是回显它吗?

 ?(((mount\">\$)(.*?)(?=)

I have solved it, and I feel a bit foolish about the answer.我已经解决了,感觉这个答案有点傻。 My bad.我的错。

Somewhere else in my code I had this在我的代码的其他地方我有这个

$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';

I have no idea why this gave the result it did, rather than a straight up error, but once removed it all is fine.我不知道为什么这会给出结果,而不是直接错误,但是一旦删除它就一切正常。 The bottom line is that both htmlspecialcharacters and htmlentities give the right answer, Lesson learnt.底线是 htmlspecialcharacters 和 htmlentities 都给出了正确的答案,经验教训。 Check all the code, my mistake was in the use of arrays, defining $regex as an array and a variable, not as I first thought here.检查所有代码,我的错误是在使用 arrays 时,将 $regex 定义为数组和变量,而不是我在这里首先想到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM