1

我正在输出一个由几个不同部分组装而成的字符串,其中一些部分可能包含也可能不包含一些 HTML。如果我应用ucfirst()到字符串并且在要显示的文本之前有 HTML,那么文本不会得到正确的大写。

$output = $before_text . $text . $after_text;

所以如果我有

$before_text = 'this is the lead into ';
$text = 'the rest of the sentence';
$after_text = '.';

然后ucfirst()工作正常,$output将是

这是句子其余部分的导引。

但是这个

$before_text = '<p>';
$text = '<a href="#">the sentence</a>.';
$after_text = '</p>';

不会做任何事情。所以我想我需要一个函数或正则表达式来获取第一个实际的常规文本,然后将其大写。但我无法弄清楚。

4

1 回答 1

3
  1. 在 $text 中使用 strip_tags 并保存在 $temp 中:这应该会为您提供不是 html 的文本。
  2. 在 $temp 上应用 ucfirst 并将其命名为 $temp_ucfirst:这应该给你字符串大写。
  3. 使用 str_replace 将 $text 中的 $temp 替换为 $temp_ucfirst:这应该将非 html 文本替换为大写的文本。
于 2018-02-20T03:16:48.977 回答