检查字符串是否定义格式并获取其中的一部分(Check string for defined format and get part of it)
如何检查字符串是否具有格式[group|any_title]并将标题返回给我?
[group|This is] -> This is [group|just an] -> just an [group|example] -> example我会用explode和[group| 作为分隔符并删除最后一个] 。 如果(爆炸的)长度> 0,则字符串具有正确的格式。
但我认为这不是一个好方法,不是吗?
How can I check if a string has the format [group|any_title] and give me the title back?
[group|This is] -> This is [group|just an] -> just an [group|example] -> exampleI would do that with explode and [group| as the delimiter and remove the last ]. If length (of explode) is > 0, then the string has the correct format.
But I think that is not quite a good way, isn't it?
最满意答案
所以你想检查字符串是否匹配正则表达式?
if(preg_match('/^\[group\|(.+)\]$/', $string, $m)) { $title = $m[1]; }如果group部分也应该是动态的:
if(preg_match('/^\[(.+)\|(.+)\]$/', $string, $m)) { $group = $m[1]; $title = $m[2]; }So you want to check if a string matches a regex?
if(preg_match('/^\[group\|(.+)\]$/', $string, $m)) { $title = $m[1]; }If the group part is supposed to be dynamic as well:
if(preg_match('/^\[(.+)\|(.+)\]$/', $string, $m)) { $group = $m[1]; $title = $m[2]; }更多推荐
发布评论