Extracting All Values Between Curly Braces Regex PHP

Extracting all values between curly braces regex php

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
)

Matching all contents between curly braces - Regex

You can use this recursive regex in PHP:

$regex = '/ { ( (?: [^{}]* | (?R) )* ) } /x';

RegEx Demo

Extracting all values between curly braces regex php (even press enter)

Use /s for multiline :

preg_match_all('/{{(.*?)}}/s', $text, $newText);

newText[1][0] is abc.

Regex extract string between 2 curly braces

And for the second operations you need it could be something this way:

$str = "Dear {{name||email}}, You are being invited for the following event: {{event}}. Regards, {{author}}";

// $data['name'] = 'John Doe';
$data['email'] = 'JohnDoe@unknown.com';
$data['event'] = 'Party yay!';
$data['author'] = 'Kehke Lunga';

$pattern = '/{{(.*?)[\|\|.*?]?}}/';

$replace = preg_replace_callback($pattern, function($match) use ($data)
{
$match = explode('||',$match[1]);

return isset($data[$match[0]]) ? $data[$match[0]] : $data[$match[1]] ;
}, $str);

echo $replace;

Basically by editing the '$pattern', and then find the correct logic needed inside the callback.

Extracting substrings between curly brackets inside a string into an array using PHP

You could use this regex to capture the strings between {}

\{([^}]*)\}

Explanation:

  • \{ Matches a literal {
  • ([^}]*) Capture all the characters not of } zero or more times. So it would capture upto the next } symbol.
  • \} Matches a literal }

Your code would be,

<?php
$regex = '~\{([^}]*)\}~';
$string = "www.example.com/?foo={foo}&test={test}";
preg_match_all($regex, $string, $matches);
var_dump($matches[1]);
?>

Output:

array(2) {
[0]=>
string(3) "foo"
[1]=>
string(4) "test"
}

DEMO

Regex pattern to get string between curly braces

You can use this recursive regex pattern in PHP:

$re = '/( { ( (?: [^{}]* | (?1) )* ) } )/x'; 
$str = "The quick brown {fox, dragon, dinosaur} jumps over the lazy {dog, cat, bear, {lion, tiger}}.";

preg_match_all($re, $str, $matches);
print_r($matches[2]);

RegEx Demo

Regex pattern extract string between curly braces and exclude curly braces

I have got a regex for my requirement.

$str = preg_replace('/\\\\add\s*\[.*]\s*{(.*?)\\\\end{(.[^\s]*?)}}/s', "$1\\end{\$2}", $str);

Working demo

Extract string between curly braces using str_extract_all

You can use

str_extract_all(script, "(?ms)^myFunction\\(\\{.*?^\\}\\)$")

Details:

  • (?ms) - turn on multiline (m, makes ^ and $ match start and end of lines respectively) and dotall (s, makes . also match line break chars that it does not match by default) modes
  • ^ - start of a line
  • myFunction\\(\\{ - a literal myFunction({ text
  • .*? - any zero or more chars, as few as possible
  • ^ - start of a line
  • \}\) - a literal }) text
  • $ - end of a line.


Related Topics



Leave a reply



Submit