文本替换:PHP/regex
Text replacement: PHP/regex
我在查看源代码模式下看到了一个与此类似的 HTML 文档(为简洁起见,下面进行了简化):
<html>
<head>
<title>System version: {{variable:system_version}}</title>
</head>
<body>
<p>You are using system version {{variable:system_version}}</p>
{{block:welcome}}
<form>
<input value="System version: {{variable:system_version}}">
<textarea>
You are using system version {{variable:system_version}}.
</textarea>
</form>
</body>
</html>
我写了一些函数可以替换这些{{...}}
类型的字符串,但是需要有选择地替换。
在上面的示例中,我希望它在 <title>
和 <p>
中被替换,但不在 <input>
和 <textarea>
中替换,因为这是用户提供的输入,将通过所见即所得的编辑器或表单插入,并且必须按从用户收到的那样保存。 {{block:welcome}}
也必须替换为它包含的任何内容。
渲染我的输出时,我会对其进行清理,然后结果应该是这样的:
<html>
<head>
<title>System version: 6.0</title>
</head>
<body>
<p>You are using system version 6.0</p>
<div>
This was the content of the welcome block.
</div>
<form>
<input value="System version: {{variable:system_version}}">
<textarea>
You are using system version {{variable:system_version}}.
</textarea>
</form>
</body>
</html>
这是我试过的。对于下面的代码,$var的值为'6.0',$val的值为'{{variable:system_version}}',$data为要搜索的整个字符串:
if (!preg_match('/<textarea|<input|<select(.+?)' . $val . '(.+?)<\/textarea|<\/input|<\/select\>/s', $data)) {
$data = str_replace($val, $var, $data);
}
请告知我的正则表达式有什么问题,因为它目前没有替换任何东西,所以 if
条件永远不会匹配。如果我在没有 if
的情况下执行 str_replace
,则在所有情况下都会进行替换。
编辑 1
在@Emma 的帮助下,替换仍然无效。下面是按原样进行替换的代码:
function replace_variable($matches, $data)
{
$ci =& get_instance();
if (!empty($matches['variable_matches'])) {
foreach ($matches['variable_matches'][0] as $key => $val) {
$vals = explode(':', $val);
$ci->load->module('core');
$var = $ci->core->get_variable(rtrim($vals[1], '}}'));
$re1 = '/<(?:textarea|select)[\s\S]*?>[\s\S]*?(' . $val . ')[\s\S]*?<\/(?:textarea|select)>/';
$re2 = '/<(?:input)[\s\S]*?(' . $val . ')[\s\S]*?>/';
if (!preg_match($re1, $data) && !preg_match($re2, $data)) {
$data = str_replace($val, $var, $data);
}
}
}
return $data;
}
这是通过 preg_match 找到的匹配项的输出值,然后我尝试通过 str_replace 替换,其中不在表单标记内 (select/textarea/input)。
Array
(
[0] => Array
(
[0] => {{variable:system_version}}
[1] => {{variable:system_version}}
[2] => {{variable:system_version}}
[3] => {{variable:system_version}}
)
[1] => Array
(
[0] => system_version
[1] => system_version
[2] => system_version
[3] => system_version
)
)
所以 - 在我尝试替换的页面上有四个匹配项,其中两个在表单标签内,另外两个不在。检查是在缓冲的整个输出上完成的,并且包含所有四个元素,但不知何故, preg_match 触发了所有这些元素,尽管有正则表达式。知道我做错了什么吗?
我的猜测是您可能正在设计类似于以下内容的表达式:
<(?:textarea|select)[\s\S]*?({{variable:system_version}})[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>
您可能想要修改它,然后替换为您想要替换的内容。
表达式在 regex101.com, if you wish to explore/simplify/modify it, and in this link 的右上面板进行了解释,如果您愿意,您可以观察它如何与一些示例输入匹配。
测试
$re = '/<(?:textarea|select)[\s\S]*?({{variable:system_version}})[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>/m';
$str = '<html>
<head>
<title>System version: 6.0</title>
</head>
<body>
<p>You are using system version 6.0</p>
<div>
This was the content of the welcome block.
</div>
<form>
<input value="System version: {{variable:system_version}}">
<textarea>
You are using system version {{variable:system_version}}.
</textarea>
</form>
</body>
</html>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
正则表达式电路
jex.im 可视化正则表达式:
编辑两步:
<(?:textarea|select)[\s\S]*?>[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?>
Demo 1
^<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>$
Demo 2
^<(?:input).*?({{variable:system_version}}).*?>$
Demo 3
我正要 post 在 上回答,但 Casimir 在我有机会之前关闭了它。为了研究人员和您的利益,我回到这里 post 适当的 html 解析然后替换技术。
代码:(Demo)
define('LOOKUP', [
'block' => [
'welcome-intro' => 'custom intro'
],
'variable' => [
'contact-email-address' => 'mmu@mmu.com',
'system_version' => 'sys ver',
'system_name' => 'sys name',
'system_login' => 'sys login',
'activate_url' => 'some url'
],
]);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//*[not(self::textarea or self::select or self::input) and contains(., '{{{')]/text()") as $node) {
$node->nodeValue = preg_replace_callback('~{{{([^:]+):([^}]+)}}}~', function($m) {
return LOOKUP[$m[1]][$m[2]] ?? '**unknown variable**';
},
$node->nodeValue);
}
echo $dom->saveHTML();
输出:
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Test</title></head><body>
<section id="about"><div class="container about-container">
<div class="row">
<div class="col-md-12">
custom intro
</div>
</div>
</div>
</section><section id="services"><div class="container">
<div class="row">
<div class="col-md-12">
<p>You are using system version: sys ver</p>
<p>Your address: mmu@mmu.com</p>
<form action="http://k.loc/content/view/welcome" class="default-form" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="hidden" name="csrfkcmstoken" value="94ee71ada809b9a79d1b723c81020c78"><div class="row">
<div class="col-sm-12 form-error"></div>
</div>
<div class="row"><div class="col-sm-12"><fieldset id="personalinfo"><legend>Personal information</legend><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testinput">Name<span class="form-validation-required"> * </span></label>
</div>
<div class="hint-text">Enter at least 2 characters and a maximum of 12 characters.</div><input id="testinput" name="testinput" placeholder="Enter your name here." class="input-group width-50" type="text" value="{{{variable:system_name}}} {{{variable:system_login}}}"><div class="row"><div class="col-sm-12"><div class="form-error"></div></div></div></div></div><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testpassword">Password</label>
</div>
<div class="hint-text">Your password must be at least 12 characters long, contain 1 special character, 1 nunber, 1 lower case character and 1 upper case character.</div><input id="testpassword" name="testpassword" placeholder="Enter your password here." class="input-group width-50" type="password"><div class="row"><div class="col-sm-12"><div class="form-error"></div></div></div></div></div></fieldset></div></div><div class="row"><div class="col-sm-12"><fieldset id="bioinfo"><legend>Biographical information</legend><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testtextarea">Biography</label>
<span class="hint-text">A minimum of 40 characters and a maximum of 255 is allowed. This hint is displayed inline.</span>
</div>
<textarea id="testtextarea" name="testtextarea" placeholder="Please enter your biography here." class="input-group-wide width-100" rows="5" cols="80">{{{variable:system_name}}}
{{{variable:system_login}}}</textarea><div class="row"><div class="col-sm-12"><div class="form-error"></div></div></div></div></div><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testsummernote">Interests</label>
<span class="hint-text">A minimum of 40 characters is required. This hint is displayed inline.</span>
</div>
<textarea id="testsummernote" name="testsummernote" class="wysiwyg-editor" placeholder="Please enter your interests here."><p>sys name<br></p><p>sys login</p><p>some url<br></p></textarea></div></div></fieldset></div></div><div class="row"><div class="col-sm-12"><button name="testsubmit" id="testsubmit" type="submit" class="btn primary">Submit<i class="zmdi zmdi-arrow-forward"></i></button></div></div>
</form> </div>
</div>
</div>
</section></body></html>
没有太多技巧。
用 DOMDocument 解析 HTML 并用 XPath 编写过滤查询,要求节点不是 textarea|select|input 标签,并且它们必须包含 {{{
在他们的文本中。将有几种 "magical" 方法来过滤 dom -- 这只是 我觉得 efficient/direct 的一种方法。
我使用preg_replace_callback()
基于查找数组执行替换。
为了避免在回调语法中使用 use()
,我通过将查找声明为常量(我无法想象您需要将其作为变量来使查找在回调的范围内可用无论如何)。
我在测试期间发现 DOMDocument 不喜欢 <section>
标签,所以我用 libxml_use_internal_errors(true);
.
消除了抱怨
我在查看源代码模式下看到了一个与此类似的 HTML 文档(为简洁起见,下面进行了简化):
<html>
<head>
<title>System version: {{variable:system_version}}</title>
</head>
<body>
<p>You are using system version {{variable:system_version}}</p>
{{block:welcome}}
<form>
<input value="System version: {{variable:system_version}}">
<textarea>
You are using system version {{variable:system_version}}.
</textarea>
</form>
</body>
</html>
我写了一些函数可以替换这些{{...}}
类型的字符串,但是需要有选择地替换。
在上面的示例中,我希望它在 <title>
和 <p>
中被替换,但不在 <input>
和 <textarea>
中替换,因为这是用户提供的输入,将通过所见即所得的编辑器或表单插入,并且必须按从用户收到的那样保存。 {{block:welcome}}
也必须替换为它包含的任何内容。
渲染我的输出时,我会对其进行清理,然后结果应该是这样的:
<html>
<head>
<title>System version: 6.0</title>
</head>
<body>
<p>You are using system version 6.0</p>
<div>
This was the content of the welcome block.
</div>
<form>
<input value="System version: {{variable:system_version}}">
<textarea>
You are using system version {{variable:system_version}}.
</textarea>
</form>
</body>
</html>
这是我试过的。对于下面的代码,$var的值为'6.0',$val的值为'{{variable:system_version}}',$data为要搜索的整个字符串:
if (!preg_match('/<textarea|<input|<select(.+?)' . $val . '(.+?)<\/textarea|<\/input|<\/select\>/s', $data)) {
$data = str_replace($val, $var, $data);
}
请告知我的正则表达式有什么问题,因为它目前没有替换任何东西,所以 if
条件永远不会匹配。如果我在没有 if
的情况下执行 str_replace
,则在所有情况下都会进行替换。
编辑 1
在@Emma 的帮助下,替换仍然无效。下面是按原样进行替换的代码:
function replace_variable($matches, $data)
{
$ci =& get_instance();
if (!empty($matches['variable_matches'])) {
foreach ($matches['variable_matches'][0] as $key => $val) {
$vals = explode(':', $val);
$ci->load->module('core');
$var = $ci->core->get_variable(rtrim($vals[1], '}}'));
$re1 = '/<(?:textarea|select)[\s\S]*?>[\s\S]*?(' . $val . ')[\s\S]*?<\/(?:textarea|select)>/';
$re2 = '/<(?:input)[\s\S]*?(' . $val . ')[\s\S]*?>/';
if (!preg_match($re1, $data) && !preg_match($re2, $data)) {
$data = str_replace($val, $var, $data);
}
}
}
return $data;
}
这是通过 preg_match 找到的匹配项的输出值,然后我尝试通过 str_replace 替换,其中不在表单标记内 (select/textarea/input)。
Array
(
[0] => Array
(
[0] => {{variable:system_version}}
[1] => {{variable:system_version}}
[2] => {{variable:system_version}}
[3] => {{variable:system_version}}
)
[1] => Array
(
[0] => system_version
[1] => system_version
[2] => system_version
[3] => system_version
)
)
所以 - 在我尝试替换的页面上有四个匹配项,其中两个在表单标签内,另外两个不在。检查是在缓冲的整个输出上完成的,并且包含所有四个元素,但不知何故, preg_match 触发了所有这些元素,尽管有正则表达式。知道我做错了什么吗?
我的猜测是您可能正在设计类似于以下内容的表达式:
<(?:textarea|select)[\s\S]*?({{variable:system_version}})[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>
您可能想要修改它,然后替换为您想要替换的内容。
表达式在 regex101.com, if you wish to explore/simplify/modify it, and in this link 的右上面板进行了解释,如果您愿意,您可以观察它如何与一些示例输入匹配。
测试
$re = '/<(?:textarea|select)[\s\S]*?({{variable:system_version}})[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>/m';
$str = '<html>
<head>
<title>System version: 6.0</title>
</head>
<body>
<p>You are using system version 6.0</p>
<div>
This was the content of the welcome block.
</div>
<form>
<input value="System version: {{variable:system_version}}">
<textarea>
You are using system version {{variable:system_version}}.
</textarea>
</form>
</body>
</html>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
正则表达式电路
jex.im 可视化正则表达式:
编辑两步:
<(?:textarea|select)[\s\S]*?>[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?>
Demo 1
^<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>$
Demo 2
^<(?:input).*?({{variable:system_version}}).*?>$
Demo 3
我正要 post 在
代码:(Demo)
define('LOOKUP', [
'block' => [
'welcome-intro' => 'custom intro'
],
'variable' => [
'contact-email-address' => 'mmu@mmu.com',
'system_version' => 'sys ver',
'system_name' => 'sys name',
'system_login' => 'sys login',
'activate_url' => 'some url'
],
]);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//*[not(self::textarea or self::select or self::input) and contains(., '{{{')]/text()") as $node) {
$node->nodeValue = preg_replace_callback('~{{{([^:]+):([^}]+)}}}~', function($m) {
return LOOKUP[$m[1]][$m[2]] ?? '**unknown variable**';
},
$node->nodeValue);
}
echo $dom->saveHTML();
输出:
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Test</title></head><body>
<section id="about"><div class="container about-container">
<div class="row">
<div class="col-md-12">
custom intro
</div>
</div>
</div>
</section><section id="services"><div class="container">
<div class="row">
<div class="col-md-12">
<p>You are using system version: sys ver</p>
<p>Your address: mmu@mmu.com</p>
<form action="http://k.loc/content/view/welcome" class="default-form" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="hidden" name="csrfkcmstoken" value="94ee71ada809b9a79d1b723c81020c78"><div class="row">
<div class="col-sm-12 form-error"></div>
</div>
<div class="row"><div class="col-sm-12"><fieldset id="personalinfo"><legend>Personal information</legend><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testinput">Name<span class="form-validation-required"> * </span></label>
</div>
<div class="hint-text">Enter at least 2 characters and a maximum of 12 characters.</div><input id="testinput" name="testinput" placeholder="Enter your name here." class="input-group width-50" type="text" value="{{{variable:system_name}}} {{{variable:system_login}}}"><div class="row"><div class="col-sm-12"><div class="form-error"></div></div></div></div></div><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testpassword">Password</label>
</div>
<div class="hint-text">Your password must be at least 12 characters long, contain 1 special character, 1 nunber, 1 lower case character and 1 upper case character.</div><input id="testpassword" name="testpassword" placeholder="Enter your password here." class="input-group width-50" type="password"><div class="row"><div class="col-sm-12"><div class="form-error"></div></div></div></div></div></fieldset></div></div><div class="row"><div class="col-sm-12"><fieldset id="bioinfo"><legend>Biographical information</legend><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testtextarea">Biography</label>
<span class="hint-text">A minimum of 40 characters and a maximum of 255 is allowed. This hint is displayed inline.</span>
</div>
<textarea id="testtextarea" name="testtextarea" placeholder="Please enter your biography here." class="input-group-wide width-100" rows="5" cols="80">{{{variable:system_name}}}
{{{variable:system_login}}}</textarea><div class="row"><div class="col-sm-12"><div class="form-error"></div></div></div></div></div><div class="row"><div class="col-sm-12">
<div class="control-label">
<label for="testsummernote">Interests</label>
<span class="hint-text">A minimum of 40 characters is required. This hint is displayed inline.</span>
</div>
<textarea id="testsummernote" name="testsummernote" class="wysiwyg-editor" placeholder="Please enter your interests here."><p>sys name<br></p><p>sys login</p><p>some url<br></p></textarea></div></div></fieldset></div></div><div class="row"><div class="col-sm-12"><button name="testsubmit" id="testsubmit" type="submit" class="btn primary">Submit<i class="zmdi zmdi-arrow-forward"></i></button></div></div>
</form> </div>
</div>
</div>
</section></body></html>
没有太多技巧。
用 DOMDocument 解析 HTML 并用 XPath 编写过滤查询,要求节点不是 textarea|select|input 标签,并且它们必须包含
{{{
在他们的文本中。将有几种 "magical" 方法来过滤 dom -- 这只是 我觉得 efficient/direct 的一种方法。我使用
preg_replace_callback()
基于查找数组执行替换。为了避免在回调语法中使用
use()
,我通过将查找声明为常量(我无法想象您需要将其作为变量来使查找在回调的范围内可用无论如何)。我在测试期间发现 DOMDocument 不喜欢
<section>
标签,所以我用libxml_use_internal_errors(true);
. 消除了抱怨