0

here is what I am trying to do. I have titles to pages. I'm trying to capitalize the last letter of the first word in each string

Examples:

hellO

hellO how are you

I can get it to work with one word but I cant figure out how to do it if there is more than one word. Any help would be great!!

Thanks so much!

4

3 回答 3

1

尝试这个

<?php

    $title                  = "Hello World";
    list($firstword, $rest) = explode(" ", $title, 2);
    $firstword              = strrev(ucfirst(strrev($firstword)));
    $title                  = $firstword . " " . $rest;

    print $title;

如果您想了解有关任何功能的更多信息,请参阅explodestrrevlistucfirst

于 2011-08-01T08:44:32.173 回答
1

由于您知道如何使用 1 个单词,因此您只需要获取第一个单词,然后输入您的算法。

  1. 尝试preg_replace_callback_"/^(\w+)/"
  2. 替换回调方法中的最后一个字符。

preg_replace_callback:http://php.net/manual/en/function.preg-replace-callback.php

更新 - 工作代码:

$string = "This is a test";
$string = preg_replace_callback(
        '/^(\w+)/',
        create_function(
            '$matches',
            'return yourUCLastAlgorithm($matches[0]);'
        ),
        $string
    );
echo $string;

UPDATE2 - 使用 preg_replace 和 e 修饰符:

$string = "This is a test";
$string = preg_replace(
        '/^(\w+)/e',
        'yourUCLastAlgorithm("$1")',
        $string
    );
echo $string;
于 2011-08-01T08:45:01.403 回答
-1

尝试这个:

$string = preg_replace('/^([ ]+)?([^ ]*)([a-z])?(.*)?$/i', "$1.$2.strtoupper($3).$4", $string);
于 2011-08-01T08:46:05.357 回答