Reputation: 9120
I have a question related to pcre with php.
I have to write a little parser for a specific file and I don't know regex very well so I need some help.
This is input...
blah blah [{img:xyzname}] one more blah blah[{root}]
and [{php:<?php ...echo 'phpcode';......?>}]
...so I have to replace these values with my ones, so here is what I did...
$alt=preg_replace_callback('|\[{(.*[^}\]])}\]|iU',
function ($match){
$m=explode(':',$match[1]);
switch($m[0]){
case 'img':
return $m[1]
break;
case 'php':
// i want that php code here in $m[1] but i am getting nothing
break;
default:
return 'UNDEFINED';
break;
}
}
},$this->content);
$this->content=$alt;
I tried many times with many patters and many tests, but I noticed that I was getting null or nothing, only when my [{php:<?php .....?>}] contains <? and only when I put < ?
.
Upvotes: 0
Views: 225
Reputation: 4336
preg_match_all('/\[{(\w+):((?:(?!}]).)*)}]/', $this->content, $matches);
print(htmlspecialchars(var_export($matches, true)));
If you're trying this in a browser remember that < and > get treated as tags unless you replace them with < and >. That's why I used htmlspecialchars()
. Using your input I get this:
array (
0 =>
array (
0 => '[{img:xyzname}]',
1 => '[{php:<?php ...echo \'phpcode\';......?>}]',
),
1 =>
array (
0 => 'img',
1 => 'php',
),
2 =>
array (
0 => 'xyzname',
1 => '<?php ...echo \'phpcode\';......?>',
),
)
Upvotes: 1