1

我究竟做错了什么?

$title = get_the_title();
$firstLetter = $title[0];
$title[0] = '<span class = "wrapBlue">' . $firstLetter . '</span>';     

echo $title; // comes out with weird switched around string?

get_the_title()是一个wordpress功能。

var_dump on $title给出一个长度为 21 的字符串。

var_dump on $firstLetter给出一个包含长度为 1 的正确字符的字符串

4

2 回答 2

5

如您所知$title[0],指的是第一个字母-但您要分配的不是字母。尝试这样的事情:

$title = '<span class = "wrapBlue">' . $firstLetter . '</span>' . substr($title, 1);
于 2013-01-16T13:16:15.797 回答
0

试试这个:

$title = get_the_title();
$firstLetter = substr($title, 0, 1);
$title .= '<span class = "wrapBlue">' . $firstLetter . '</span>' . substr($title, 1, strlen($title);
echo $title;

单线:

echo '<span class = "wrapBlue">' . substr(get_the_title(), 0, 1) . '</span>' . substr(get_the_title(), 1, strlen(get_the_title());
于 2013-01-16T13:14:52.243 回答