【发布时间】:2021-11-30 04:37:18
【问题描述】:
我有以下一段文字:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Lorem Ipsum has been the industry's</p><h1>standard dummy text</h1><p>ever since the 1500s</p>
<p>When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries</p>
我需要在字符串 standard dummy text 出现之前将一个类 (inline) 添加到开头的 <p> 标记。或者,如果需要/更容易,它可以在<h1>standard dummy text<h1> 中使用 h1 标记?这样我的文字就变成了:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p class="inline">Lorem Ipsum has been the industry's</p><h1 class="inline">standard dummy text</h1> <p>ever since the 1500s</p>
<p class="inline">When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries</p>
原因是我目前必须动态地将 h1 标记放入某些文本中。这可行,但是我需要预先在段落中添加一个自定义 inline 类(它只是将显示设置为内联),以便段落和标题位于同一行。
public static function markupH1TagWithinText($h1, $text)
{
$pos = strpos($text, $h1);
if ($pos !== false) {
$replacement = '</p><h1 class="inline">' . $h1 . '</h1><p class="inline">';
return substr_replace($text, $replacement, $pos, strlen($h1));
}
return false;
}
【问题讨论】:
-
你检查
str_spilt函数。 -
您不能仅通过查看
</p><h1>standard dummy text</h1>开始的位置来执行此操作 - 您需要从那里返回并在该方向上找到第一个先前的<p>,因为那是您需要替换的标签之一。 (而且整个事情真的宁愿是 DOM 解析器的工作,而不是字符串替换或正则表达式。) -
我也会使用 DOM 解析器。但我不确定:为什么最后一个
<p>标签会得到一个inlineCSS 类?添加<h1>标签是工作的一部分吗?它已经存在于输入文本中。 -
是否需要在受影响的
<h1>之后的每个<p>中添加inline类?还是只是第一个? -
@EnricoDias 只是之后的第一个
标签: php string str-replace strpos