简体   繁体   English

php用正则表达式查找字符串

[英]Php find string with regex

I've read multiple tutorials on regex but it just won't stick in my head.我已经阅读了多个关于正则表达式的教程,但它不会留在我的脑海中。 I can never get my patterns to work.我永远无法让我的模式发挥作用。 Hope someone can help.希望有人可以提供帮助。

I have a php variable ($content) where I need to find a certain pattern that looks like this我有一个 php 变量 ($content),我需要在其中找到一个看起来像这样的特定模式

[gallery::name\/of\/the\/folder\/] [画廊::名称\/的\/文件夹\/]

I would like to search:我想搜索:

- starting with "[gallery::"
- any other character (variable length)
- ending with "]"

Try capturing it: 尝试捕获它:

preg_match("/\[gallery::(.*?)]/",$content,$m);

Now $m is an array: 现在$m是一个数组:

0 => [gallery::/name/of/the/folder/]
1 => /name/of/the/folder/

change your regex to 把你的正则表达式改为

'/\[gallery::([A-Za-z\/]+)\]/'

Since I put the folder/path part in parenthesis, you should get a capture group out of it. 由于我将文件夹/路径部分放在括号中,您应该从中获取一个捕获组。

if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM