【发布时间】:2020-10-23 21:57:12
【问题描述】:
我一直在尝试将字符串的第一个字母放在大写字母中,但无法正常工作。
我已经尝试了以下代码:
<?php
$str = $_POST['Papier'];
$f = highlightKeywords('papierwaren', $str);
$s = strtolower($f);
$r = ucfirst($s);
function highlightKeywords($text, $keyword)
{
$pos = strpos($text, $keyword);
$wordsAry = explode(" ", $keyword);
$wordsCount = count($wordsAry);
for ($i = 0; $i < $wordsCount; $i++) {
if ($pos === false) {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . strtolower($wordsAry[$i]) . "</span>";
} else {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . $wordsAry[$i] . "</span>";
}
$text = str_ireplace($wordsAry[$i], $highlighted_text, $text);
}
return $text;
}
仍然,我没有让它工作,我尝试了以下是否出现空格
$r=ucfirst(trim($s));
仍然没有成功。这个“纸制品”文本我是从数据库中得到的,所以请有人帮我解决这个问题。
【问题讨论】:
-
$str 是与我们在搜索框中使用的纸制品相关的任何关键字,例如 pap 或 papier 或 Papier 等。
-
在条件下转换
-
我也试过它也不起作用......@SahilGupta
-
您似乎将 html 添加到您的字符串 (
<span ...)。当您使用ucfirst时,它会将第一个字符更改为大写,但第一个字符现在是<,不幸的是(或幸运的是?),<的大写是<。尝试在highlightKeywords函数中添加ucfirst -
@Kaddath $highlighted_text 还是在 highlightKeywords 函数内部,如 $text?
标签: php html string laravel uppercase