PHP preg_replace:如何替换标记之间的文本?

PHP preg_replace:如何替换标记之间的文本?,php,preg-replace,Php,Preg Replace,我有一个函数doit(),它以这种方式转换文本: I like berries -> I LikE BerrieS. 我需要它来处理html文本。 如何仅在html标记之间转换文本?不要触摸标签名称和所有标签属性。例如,我需要: <p class="super green" style="height: auto">I LikE BerrieS</p> 我喜欢浆果 但不是: <P ClasS="SupeR GreeN" StylE="HeighT: Au

我有一个函数doit(),它以这种方式转换文本:

I like berries -> I LikE BerrieS.
我需要它来处理html文本。 如何仅在html标记之间转换文本?不要触摸标签名称和所有标签属性。例如,我需要:

<p class="super green" style="height: auto">I LikE BerrieS</p>
我喜欢浆果

但不是:

<P ClasS="SupeR GreeN" StylE="HeighT: AutO">I LikE BerrieS</P>

我喜欢浆果

我尝试过简单的preg_replace()模式,但没有任何效果。我不熟悉regexp,需要帮助

也许preg_match()会更好?
有什么建议吗?如果能提供可用的php代码就好了。

$text='其他一些文本
$text = '<div>some other text</div> 
<p class="super green" style="height: auto">i like berries</p>';

//this preg is searching for tags and text inside it
//and then change all first words to upper
$text = preg_replace_callback('#(<.*?>)(.*?)(</.*?>)#', function($matches){

  //this preg is searching for last letters in words and changing it to upper
  $t = preg_replace_callback('#([^ ])( |$)#', function($matches2){
    return strtoupper($matches2[1]) . $matches2[2];
  }, ucwords($matches[2]));
  return $matches[1] . $t . $matches[3];
}, $text);

var_dump($text);
我喜欢浆果; //此preg正在搜索其中的标记和文本 //然后将所有第一个单词改为大写 $text=preg_replace_回调('#()(.*)()#',函数($matches){ //此preg正在搜索单词中的最后一个字母,并将其更改为大写 $t=preg_replace_回调('#([^])(|$)#',函数($matches2){ 返回strtoupper($matches2[1])。$matches2[2]; },ucwords($matches[2]); 返回$matches[1]。$t.$matches[3]; }美元文本); 变量转储($text);
明确您希望从我们这里得到什么?最好提供可用的php代码。到目前为止,您尝试了什么?请相应地编辑您的问题。@Somerussian如果您正在等待有人为您提供处理嵌套标记的解决方案,请使用一个经过深思熟虑的示例字符串更新您的问题,该字符串将表达您的逻辑,并将您的html字符串的预期输出与嵌套标记一起发布。这似乎是DomDocument和/或XPath的工作。。。。给出-1,因为即使答案正确,也没有评论。。。好极了!添加评论…好的,谢谢你的解释。奇怪的是,人们可以匿名在这里做事情,你甚至不能讨论“为什么”。太棒了!但它不适用于内部标记,例如:
一些其他文本

我喜欢浆果

您可以修复它吗?在标记中有标记。你没有提到那种情况。好。。。我会努力的。我现在放弃。我之前没有使用递归模式,我需要先了解一点,但现在我必须在工作中完成我的工作。