<?php
$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';
preg_match_all(
'~(?<={).+?(?=})~',
$string,
$matches
);
print_r(
$matches
);
/*
Array
(
[0] => Array
(
[0] => content inside first bracket
[1] => content inside second bracket
)
)
*/
So do you want to extract the characters between {} and replace them with something else or do you want to capture the characters between {} and do something absolutely different with those? The code provided works for capturing the substrings, and if you got stuck with replacing those with something else then you need to articulate yourself a bit clearer.
$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';
preg_match_all(
'~(?<={).+?(?=})~',
$string,
$matches
);
$i = 1;
function do_increment(&$i)
{
return '#' . $i++;
}
$string = preg_replace("~(?<={).+?(?=})~e", 'do_increment($i)', $string);
var_dump($string);
I don’t want to sound rude, but if someone showed you how to extract portion of a string, and if you couldn’t reach a conclusion how to change that substring to something else - you might want to work more with your php because this can be done with looping trough $matches and using str_replace, which we can all agree - are basics of string manipulation.
If you want to learn how to create your own regular expressions, good place to start is www.regular-expressions.info and of course, http://www.php.net/manual/en/ref.pcre.php.