1
$title = '228-example-of-the-title'

我需要将字符串转换为:

标题示例

我该怎么做?

4

6 回答 6

3

单线,

$title = '228-example-of-the-title';
ucwords(implode(' ', array_slice(explode('-', $title), 1)));
  • 这会在破折号 ( explode(token, input)) 上拆分字符串,
  • 减去第一个元素 ( array_slice(array, offset))
  • 用空格 ( implode(glue, array)) 连接结果集,
  • 最后将每个单词大写(感谢salathe)。
于 2012-05-15T20:15:00.563 回答
2
$title = '228-example-of-the-title'
$start_pos = strpos($title, '-');
$friendly_title = str_replace('-', ' ', substr($title, $start_pos + 1));
于 2012-05-15T20:11:29.940 回答
1

使用explode()拆分“-”并将字符串放入数组中

$title_array = explode("-",$title);
$new_string = "";

for($i=1; $i<count($title_array); $i++)
{
$new_string .= $title_array[$i]." ";
}

echo $new_string;
于 2012-05-15T20:10:54.043 回答
1

您可以使用以下代码执行此操作

$title = '228-example-of-the-title';

$parts = explode('-',$title);
array_shift($parts);
$title = implode(' ',$parts); 

使用的函数:explode implodearray_shift

于 2012-05-15T20:14:03.950 回答
1
$pieces = explode("-", $title);
$result = "";
for ($i = 1; $i < count(pieces); $i++) {
    $result = $result . ucFirst($pieces[$i]);
}
于 2012-05-15T20:14:44.513 回答
1
$toArray = explode("-",$title);
$cleanArray = array_shift($toArray);
$finalString = implode(' ' , $cleanArray);
// echo ucwords($finalStirng);
于 2012-05-15T20:18:57.073 回答