0

我正在使用此函数将文本转换为标题大小写:

function strtotitle($title) {

 $smallwordsarray = array( 'of','a','the','and','an','or','nor','but','is','if','then','else','when', 'at','from','by','on','off','for','in','to','into','with','it', 'as' );

  // Split the string into separate words
  $words = explode(' ', $title);

  foreach ($words as $key => $word) {

   // If this word is the first, or it's not one of our small words, capitalise it
   // with ucwords().
   if ($key == 0 or !in_array($word, $smallwordsarray))
    $words[$key] = ucwords($word);
   }

   // Join the words back into a string
   $newtitle = implode(' ', $words);

   return $newtitle;
}

问题是,如果文本是粗体、斜体等,那么该功能将不起作用,并且不会对单词进行标题。

例如:“这是一个简单的句子”将转换为“这是一个简单的句子”。但是“This is a simple sentence”将转换为“This is a simple Sentence”。

非常感谢任何帮助。

4

3 回答 3

2

如果你在谈论 HTML 标签并且你想保存它,你可以使用这个实现:

<?php
  /* Your old string */
  $string = "<b>asd</b>";

  /* Remove html code */
  $old = strip_tags($string);
  /* Upper case the first letter */
  $new = ucfirst($old);

  /* Replace old word to new word in $string */
  $real = str_replace($old,$new,$string);

  /* Here the new string and the old string */
  echo $real." ".$string;
?>

因此,您的代码中的解决方案如下所示:

function strtotitle($title) {

     $smallwordsarray = array(     'of','a','the','and','an','or','nor','but','is','if','then','else','when', 'at','from','by','on','off','for','in','to','into','with','it', 'as' );

     // Split the string into separate words
     $words = explode(' ', $title);

     foreach ($words as $key => $word) {

        // If this word is the first, or it's not one of our small words, capitalise it
        // with ucwords().
        if ($key == 0 or !in_array(strip_tags($word), $smallwordsarray)) {
             $old = strip_tags($word);
             $new = ucfirst($old);
             $words[key] = str_replace($old,$new,$word);
        }
     }

     // Join the words back into a string
     $newtitle = implode(' ', $words);

     return $newtitle;
}
于 2017-06-25T16:41:10.743 回答
0
<?php 
function strtotitle($title) {
    $smallwordsarray = array( 'of','a','the','and','an','or','nor','but','is','if','then','else','when', 'at','from','by','on','off','for','in','to','into','with','it', 'as' );
    $words = $temp = explode(' ', strip_tags($title));

    foreach ($temp as $key => $word) {
        if ($key == 0 or !in_array($word, $smallwordsarray)) $temp[$key] = ucwords($word);
    }

    foreach($words as $index => $word) $title = str_replace($word, $temp[$index], $title);
    return $title;
}

var_dump(strtotitle('This is a simple sentence'));
var_dump(strtotitle('This is a <b>simple</b> <i>sentence</i>'));
于 2017-06-25T16:41:48.297 回答
-1

你可以这样使用strip_tags

function strtotitle($title) {

  $smallwordsarray = array( 'of','a','the','and','an','or','nor','but','is','if','then','else','when', 'at','from','by','on','off','for','in','to','into','with','it', 'as' );

  // Remove HTML tags
  $titleWithoutTags = strip_tags($title);

  // Split the string into separate words
  $words = explode(' ', $titleWithoutTags);

  foreach ($words as $key => $word) {

   // If this word is the first, or it's not one of our small words, capitalise it
   // with ucwords().
   if ($key == 0 or !in_array($word, $smallwordsarray))
    $words[$key] = ucwords($word);
   }

   // Join the words back into a string
   $newtitle = implode(' ', $words);

   return $newtitle;
}
于 2017-06-25T16:45:02.993 回答