【问题标题】:make first letter caps in php but ucfirst(strtolower('string')) does not work在 php 中设置首字母大写,但 ucfirst(strtolower('string')) 不起作用
【发布时间】: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 添加到您的字符串 (&lt;span ...)。当您使用ucfirst 时,它会将第一个字符更改为大写,但第一个字符现在是&lt;,不幸的是(或幸运的是?),&lt; 的大写是&lt;。尝试在highlightKeywords函数中添加ucfirst
  • @Kaddath $highlighted_text 还是在 highlightKeywords 函数内部,如 $text?

标签: php html string laravel uppercase


【解决方案1】:

正如Kaddath 所说,您正在将HTML 添加到您的字符串中(

试试这个代码:

<?php

$str = 'papier';

$f = highlightKeywords('papierwaren', $str);

echo $f;

function highlightKeywords($text, $keyword)
{

    $pos = strpos($text, $keyword);

    $wordsAry = explode(" ", $keyword);

    $wordsCount = count($wordsAry);

    for ($i = 0; $i < $wordsCount; $i++) {
        if ($pos === false) {
            if ($i === 0) {
                $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . ucfirst(strtolower($wordsAry[$i])) . "</span>";
            } else {
                $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . strtolower($wordsAry[$i]) . "</span>";
            }
        } else {
            if ($i === 0) {
                $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . ucfirst($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;
}

【讨论】:

  • 嗨,正如您所建议的,我尝试使用您的代码。它确实运行良好,但是任何其他单词之间的关键字变成了第一个字母的大写,所以我通过删除 else 中的 ucfirst 来尝试以下操作部分并添加 strtolower。
  • else { if ($i === 0) { $highlighted_text = "" 。 strtolower($wordsAry[$i]) 。 ""; } 其他 { $highlighted_text = "" 。 $wordsAry[$i] 。 ""; } }
  • 我收到这样的错误。当我输入“Cup a Soup”这个词时,它会显示一些类似以下的代码,但不是其他词->' an style='font-weight:700;color:#151313;'>cupan> a Soup'
【解决方案2】:

在 laravel 中这应该会有所帮助

use Illuminate\Support\Str;

$testString = 'this is a sentence.';

$uppercased = Str::ucfirst($testString);

不要忘记将Illuminate\Support\Str 导入控制器。

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 2013-09-26
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多