I'm trying to find a string inside curly braces that can appear in many variations.
ie In the following string i'm searching for the word Link
asdasd{Link1111:TAG}\\r\\n {TAG:Link2sds}
I'm using the following command: $pattern = "/{(Link.*?)}|{(.*Link.*?)}/";
and the result is:
{array} [3]
0 => {array} [2]
0 = "{Link1:TAG}"
1 = "{TAG:Link2}"
1 = {array} [2]
0 = "Link1:TAG"
1 = ""
2 = {array} [2]
0 = ""
1 = "TAG:Link2"
I'm expecting only the first array without the curly braces...what am i missing with the regex? thx.
preg_match_all
is global and finds all matches. If you want to find it just once use preg_match
.
Demo: https://eval.in/572825
The 0
index in your current example is all matches. The 1
is the first capture group Link.*?
, and the 2
is your second capture group .*Link.*?
.
Just use /{(.*?Link.*?)}/
and your matches will be in index 1
<?php
$str = "asdasd{Link1111:TAG}\r\n {TAG:Link2sds}";
$pattern = "/{(.*?Link.*?)}/";
preg_match_all($pattern, $str, $matches);
print_r($matches[1]);
Your eval
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.