-2

I have Three word . But i need only those word which is in { } Like:

text1 {text2} text3

get the text only in { }

Result = text2
And
result = text1 text3 (remove {} word )
and
from text1 {text2} text3 {text4}
result = text2 and text4
Ashik Kaiser
  • 35
  • 1
  • 5
  • Possible duplicate of [Extract a substring between two characters in a string PHP](https://stackoverflow.com/questions/14891743/extract-a-substring-between-two-characters-in-a-string-php) – dferenc Jan 02 '18 at 11:44

3 Answers3

1

Try preg_match_all for this :

$a = 'text1 {text2} text3';
 preg_match_all("/\\{(.*?)\\}/", $a, $matches); 
print_R($matches[1][0]);

output will be:

text2
Paritosh Mahale
  • 1,238
  • 2
  • 14
  • 42
  • I need two more result `result = text1 text3 (remove {} word )` and from text1 {text2} text3 {text4} `result = text2 and text4` – Ashik Kaiser Jan 02 '18 at 13:35
  • @AshikKaiser check `$matches` array for `$a = 'text1 {text2} {text3}';` it will return you 'Array ( [0] => Array ( [0] => {text2} [1] => {text3} ) [1] => Array ( [0] => text2 [1] => text3 ) )' – Paritosh Mahale Jan 03 '18 at 04:19
0

One solution would be loop through each character. If character is { then start collecting characters into word variable. If you find } character, then finish - you have a word.

Dariux
  • 3,953
  • 9
  • 43
  • 69
0

with regex:

$search = 'text1 {text2} text3';
preg_match('#\{(.*)\}#', $search, $match);
echo $match[1];

see preg_match doc

akabaka
  • 115
  • 1
  • 9