I have content in this form
$content =\"This is a sample text where {123456} and {7894560} [\'These are samples\']{145789}
\";
Two compact solutions weren't mentioned:
(?<={)[^}]*(?=})
and
{\K[^}]*(?=})
These allow you to access the matches directly, without capture groups. For instance:
$regex = '/{\K[^}]*(?=})/m';
preg_match_all($regex, $yourstring, $matches);
// See all matches
print_r($matches[0]);
Explanation
(?<={)
lookbehind asserts that what precedes is an opening brace.{
matches the opening brace, then \K
tells the engine to abandon what was matched so far. \K
is available in Perl, PHP and R (which use the PCRE
engine), and Ruby 2.0+[^}]
negated character class represents one character that is not a closing brace,*
quantifier matches that zero or more times(?=})
asserts that what follows is a closing brace.Reference
Do like this...
<?php
$content ="<p>This is a sample text where {123456} and {7894560} ['These are samples']{145789}</p>";
preg_match_all('/{(.*?)}/', $content, $matches);
print_r(array_map('intval',$matches[1]));
OUTPUT :
Array
(
[0] => 123456
[1] => 7894560
[2] => 145789
)
DEMO :https://eval.in/84197
$content ="<p>This is a sample text where {123456} and {7894560} ['These are samples']{145789}</p>";
preg_match_all('/{(.*?)}/', $content, $matches);
foreach ($matches[1] as $a ){
echo $a." ";
}
Output:
123456 7894560 145789