如何(仅)从PHP中的表达式中删除周围的HTML标记?

In my content management system, I'm using expressions as *|UPCOMING_EVENTS|* allow users to generate dynamic content wherever they please. The users modify / create content via CKEditor. Now, the problem is that CKEditor automatically wraps my expressions with a div or a p or both...

I would like to modify this

<h1>Title</h1>
<p>Blablabla</p>
<p>*|UPCOMING_EVENTS|*</p> 
<p>Blablabla</p>

into this

<h1>Title</h1>
<p>Blablabla</p>
*|UPCOMING_EVENTS|* 
<p>Blablabla</p>

Could anyone please help me find the way to remove only the surrounding tags (in case nested) of my expressions? Right now I'm using this technique to find and replace my expressions:

preg_match_all('/\*\|(.*?)\|\*/', $content, $matches);
foreach($matches[1] as $mid => $match) {
    $content = str_replace($matches[0][$mid],$match(),$content);    
}

Thanks in advance.

Try

preg_replace('#<([a-z0-9]+)>(\*\|.+?\|\*)</\1>#i', '$2', $subject);

edit: Maybe a bit too elaborate and only matches one tag. If p and div are both used, this regex is better:

#(<p.*>|<div.*>)+\s*(\*\|.+?\|\*)\s*(</p>|</div>)+#i

Have you tried strip_tags? I think this is what you want http://php.net/manual/en/function.strip-tags.php

if the tags are always identical you can use the trim() to strip them from your string. try it like this.

trim($str,'<div><p>');
and
trim($str,'</p></div>');