简体   繁体   English

正则表达式替换括号之间的html标签

[英]Regex to replace html tags between parenthesis

I need to replace html tags placed between parenthesis. 我需要替换放在括号之间的html标签。 Following is my code. 以下是我的代码。 Any help would be appreciated. 任何帮助,将不胜感激。

$string  = '<table><tr>Hello{<strong><br/>name<br/></strong>}</tr></table>';
echo preg_replace("/\{<.*?>\}/","",$string);

Required output is 所需的输出是

<table><tr>Hello name</tr></table>

@Diksha Try this: @Diksha试试这个:

$string  = '<table><tr><td>Hello {<strong><br/>name<br/></strong>}</td></tr></table>';


echo $str = preg_replace_callback("/\{<.*?>\}/", function($m){
    return preg_replace('/\{|\}/',"",strip_tags($m[0]));
}, $string);

You can not do this using a simple regex alone, but you can use a regex to find the paranthesis blocks as follwing 您不能仅使用一个简单的正则表达式来完成此操作,但是可以使用正则表达式来找到下面的括号

function process_paranthesis($match) {
  return strip_tags($match[1]);
}

$string  = '<table><tr>Hello { <strong>name</strong>}</tr></table>';
echo preg_replace_callback("/\{([^\}]*)\}/", "process_paranthesis",$string);

The RegEx was modified to just find all {...}-blocks and we use preg_replace_callback() , which calls a function that computes the string that the match is to be replaced to. RegEx已修改为仅查找所有{...}块,我们使用preg_replace_callback() ,该函数调用一个函数,该函数计算要替换为匹配项的字符串。 The parameter $match of the callback function contains information about the match in various ways. 回调函数的参数$ match以各种方式包含有关匹配的信息。 $match[0] contains the whole text of the match and $match[1] contains the text within the first paranthesis within the match. $ match [0]包含比赛的整个文本,$ match [1]包含比赛中第一个括号内的文本。 The function strip_tags() is then used within the callback function, to remove all HTML-Tags. 然后,在回调函数中使用函数strip_tags()删除所有HTML标签。 This is a predefined function and should be used instead of reinventing the wheel. 这是预定义的功能,应该代替重新发明轮子。

The RegEx is constructed as following: RegEx的构造如下:

  1. A match starts with a { and ends with a } ; 的匹配具有开始{并结束与} ; we need to escape it so we use \\{ ... \\} . 我们需要对其进行转义,因此我们使用\\{ ... \\}
  2. We want to process everything, but the surrounding { and } , so we put round paranthesis inside: \\{( ... )\\} and will then get the whole content within the curly braces as $match[1] without further need to remove those curly braces by using other string functions. 我们希望处理所有内容,但周围的{} )\\} ,因此我们将圆括号放在\\{( ... )\\} ,然后将花括号中的全部内容作为$ match [1]来获取,而无需进一步通过使用其他字符串函数删除那些花括号。
  3. We want to allow all characters between the { and } except for the } itself; 我们希望允许{}之间的所有字符,除了}本身; we use [^\\}] , which matches every kind of character but } ; 我们使用[^\\}] ,它匹配除}所有字符; and we want to allow multiple of them, resulting in: [^\\}]* 并且我们想允许它们多个,结果是: [^\\}]*

NOTE: .* is greedy. 注意: .*是贪婪的。 So, if we just use .* instead of [^\\}]* we would get weird results in case there are multiple blocks of curly braces. 因此,如果仅使用.*而不是[^\\}]*则在有多个花括号块的情况下,我们会得到怪异的结果。 The match would start at the first opening { and end at the last } within the string and would containing all blocks and everything between it. 匹配将从字符串中的第一个开头{开始,最后一个}结束,并将包含所有块及其之间的所有内容。 This would match like this: "Text {in first} something between {and second one} . And some more." 这将匹配如下:“ {in first} something between {and second one}短信。还有更多。” -- But we want it to match like this: "Text {in first} something between {and second one} . And some more.", right? -但我们希望它像这样匹配:“在{and second one}之间的{in first}文本中输入文本。还有更多。”对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM