我试图让我的客户端能够通过在他们的WYSIWYG编辑器中插入一个简短的代码来调用具有各种代码片段的函数.
例如,他们会写类似......
[getSnippet(1)]
Run Code Online (Sandbox Code Playgroud)
这将调用我的getSnippet($ id)php函数并输出相应的"块".
当我像这样硬编码$ id时,它有效...
echo str_replace('[getSnippet(1)]',getSnippet(1),$rowPage['sidebar_details']);
Run Code Online (Sandbox Code Playgroud)
但是,我真的想让'1'动态.我有点像......
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);
Run Code Online (Sandbox Code Playgroud)
不太正常:(