[英]php replace regular expression instead of string replace
I'm trying to give my client the ability to call a function that has various code snippets by inserted a short code in their WYSIWYG editor. 我试图让我的客户端能够通过在他们的WYSIWYG编辑器中插入一个简短的代码来调用具有各种代码片段的函数。
For example, they will write something like... 例如,他们会写类似......
[getSnippet(1)]
This will call my getSnippet($id) php function and output the appropriate 'chunk'. 这将调用我的getSnippet($ id)php函数并输出相应的“块”。
It works when I hard code the $id like this... 当我像这样硬编码$ id时,它有效...
echo str_replace('[getSnippet(1)]',getSnippet(1),$rowPage['sidebar_details']);
However, I really want to make the '1' dynamic. 但是,我真的想让'1'动态。 I'm sort of on the right track with something like...
我有点像......
function getSnippet($id) {
if ($id == 1) {
echo "car";
}
}
$string = "This [getSnippet(1)] is a sentence.This is the next one.";
$regex = '#([getSnippet(\w)])#';
$string = preg_replace($regex, '. \1', $string);
//If you want to capture more than just periods, you can do:
echo preg_replace('#(\.|,|\?|!)(\w)#', '\1 \2', $string);
Not quite working :( 不太正常:(
Firstly in your regex you need to add literal parentheses (the ones you have just capture \\w
but that will not match the parentheses themselves): 首先在你的正则表达式中你需要添加文字括号(你刚刚捕获的那些括号
\\w
但是它们与括号本身不匹配):
$regex = '#(\[getSnippet\((\w)\)\])#';
I also escaped the square brackets, otherwise they will open a character class. 我也逃过了方括号,否则他们会打开一个角色类。 Also be aware that this captures only one character for the parameter!
另请注意,这只会捕获参数的一个字符!
But I recommend you use preg_replace_callback
, with a regex like this: 但我建议你使用
preg_replace_callback
,使用这样的正则表达式:
function getSnippet($id) {
if ($id == 1) {
return "car";
}
}
function replaceCallback($matches) {
return getSnippet($matches[1]);
}
$string = preg_replace_callback(
'#\[getSnippet\((\w+)\)\]#',
'replaceCallback',
$string
);
Note that I changed the echo
in your getSnippet
to a return
. 请注意,我将
getSnippet
的echo
更改为return
。
Within the callback $matches[1]
will contain the first captured group, which in this case is your parameter (which now allows for multiple characters). 在回调中,
$matches[1]
将包含第一个捕获的组,在这种情况下是您的参数(现在允许多个字符)。 Of course, you could also adjust you getSnippet
function to read the id
from the $matches
array instead of redirecting through the replaceCallback
. 当然,您也可以调整
getSnippet
函数来从$matches
数组中读取id
,而不是通过replaceCallback
重定向。
But this approach here is slightly more flexible, as it allows you to redirect to multiple functions. 但这种方法稍微灵活一些,因为它允许您重定向到多个功能。 Just as an example, if you changed the regex to
#\\[(getSnippet|otherFunction)\\((\\w+)\\)\\]#
then you could find two different functions, and replaceCallback
could find out the name of the function in $matches[1]
and call the function with the parameter $matches[2]
. 举个例子,如果你将正则表达式更改为
#\\[(getSnippet|otherFunction)\\((\\w+)\\)\\]#
那么你可以找到两个不同的函数,而replaceCallback
可以在$matches[1]
找到函数的名称。 $matches[1]
并使用参数$matches[2]
调用该函数。 Like this: 像这样:
function getSnippet($id) {
...
}
function otherFunction($parameter) {
...
}
function replaceCallback($matches) {
return $matches[1]($matches[2]);
}
$string = preg_replace_callback(
'#\[(getSnippet|otherFunction)\((\w+)\)\]#',
'replaceCallback',
$string
);
It really depends on where you want to go with this. 这真的取决于你想要去哪里。 The important thing is, there is no way of processing an arbitrary parameter in a replacement without using
preg_replace_callback
. 重要的是,如果不使用
preg_replace_callback
,就无法在替换中处理任意参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.