小编c.d*_*ies的帖子

php替换正则表达式而不是字符串替换

我试图让我的客户端能够通过在他们的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)

不太正常:(

php regex

6
推荐指数
1
解决办法
404
查看次数

标签 统计

php ×1

regex ×1