检查字符串是否定义格式并获取其中的一部分(Check string for defined format and get part of it)

编程入门 行业动态 更新时间:2024-10-23 13:26:43
检查字符串是否定义格式并获取其中的一部分(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] -> example

I 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]; }

更多推荐

本文发布于:2023-07-05 09:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   定义   格式   Check   part

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!