用动态替换文本替换方括号占位符
我在文本中有一个占位符,如下所示:[demo Category=1]
,我想用类别号 1 的内容替换占位符,例如 这是我的第一个类别的内容< /code>
这是我的起点模式 - 这就是我所拥有的: '/[演示\s*.*?]/i';
I have a placeholder in text like this one: [demo category=1]
and I want to replace the placeholder with the content of category number 1 e.g. This is the Content of my first Category
This is my starting point pattern - that's all I have:'/[demo\s*.*?]/i';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您需要转义方括号,因为它们是 PCRE 中的特殊字符:
其次,听起来您想对末尾的数字执行某些操作,因此您需要使用括号捕获它:
大括号将捕获 < code>\d+ 并将其存储在引用中。
\d+
将仅匹配数字字符串。最后,听起来您需要使用
preg_replace_callback< /code>
对匹配项执行特殊函数以获得所需的字符串:
Firstly, you need to escape the square brackets as they are special characters in PCREs:
Secondly, it sounds like you want to do something with the digit at the end, so you'll want to capture it using parenthesis:
The braces will capture
\d+
and store it in a reference.\d+
will match a string of numbers only.Finally, it sounds like you need to use
preg_replace_callback
to perform a special function on the matches in order to get the string you want:除了上述答案之外,您还有两种方法来进行实际替换。假设您有 10 个要替换的类别名称,您可以执行类似的操作
,或者
在这两种情况下, get_category_name($id) 是一个获取 id 的类别名称的函数。您应该测试这两个选项,以评估哪个选项更适合您的使用。
further to the above answers, you have 2 ways to do the actual replacing. assuming you have 10 category names you want to replace, you can either do something like
or
in both cases, get_category_name($id) is a function that will get a category name for an id. you should test both options to evaluate which is faster for your uses.
模式将是这样的
(你需要转义括号,因为它们很特殊)
The pattern is going to be like this
(you need to escape brackets because they're special)
[
和]
字符具有特殊含义(它们表示字符类 - 字符范围和集合)。您需要将[
转义为\[
(显然,在 PHP 中,与其他正则表达式风格不同,您还需要转义]
)。另外,我建议您使用字符类[^]]
= 匹配任何不是]
的字符应该效果更好。
编辑:如果你想提取姓名和号码,那么你可以使用
The
[
and]
characters have special meaning (they denote character classes - ranges and collections of character). You need to escape[
as\[
(and evidently in PHP, unlike other regex flavors, you also need to escape]
). Also I suggest you make use of the character class[^]]
= match any character that is not a]
should work better.
Edit: If you want to extract the name and number, then you can use
解析占位符并捕获属性名称及其值。在
preg_replace_callback()
的回调中,使用获取动态替换数据所需的任何机制。如果尝试获取动态数据没有结果,只需将原始占位符替换为自身(实际上不进行任何更改)。我通常不建议使用可变变量,但不清楚替换数据来自哪里,或者是否需要容纳多个属性名称。另外,通常我更喜欢在回调中使用箭头语法,但变量变量在箭头函数语法中不起作用。
代码:(演示)
输出:
Parse the placeholder(s) and capture the attribute name and its value. In the callback of
preg_replace_callback()
use whatever mechanism is required to fetch the dynamic replacement data. If the attempt to fetch the dynamic data is fruitless, just replace the original placeholder with itself (effectively making no change).I don't generally advise the use of variable variables, but it was unclear where the replacement data was coming from or if you even need to accommodate multiple attribute names. Also, normally I prefer to use arrow syntax with callbacks, but variable variables do not work within arrow function syntax.
Code: (Demo)
Output: