问题描述:

I have HTML content of type:

<em>this is some</em> dummy text

I want to format it to: (Capitalize first letter keeping the HTML tags intact)

<em>This is some</em> dummy text

using PHP. I don't want to use

:div:first-letter {

text-transform: uppercase;

}

If I try to use strip_tags and then use ucfirst, it can't keep track of <em> tags.

网友答案:
function capsfirst_sentence($string) {return empty($string) ? '' : preg_replace('/\b(\w)/e', 'strtoupper("$1")', $string);}   

This is in php If helps you

网友答案:

I'd bet there are better ways to achieve this. But I think this snippet could be of help. It iterates through the string and ignores prefixed tags.

<?php
  $str    = "<foo>string</foo> to loop through";
  $strlen = strlen($str);
  $inTag  = false;
  for( $i = 0; $i < $strlen; $i++ ) {
    $char = substr( $str, $i, 1 );
    if ($inTag) {
      if (strcmp($char, ">") == 0) {
        $inTag = false;
      }
      continue;
    }

    if (strcmp($char, "<") == 0) {
      $inTag = true;
      continue;
    }

    substr_replace($str, strtoupper($char), $i, 1);
    break;
  }
  echo($str);
?>
网友答案:

The ::first-letter selector is used to add a style to the first letter of the specified selector.

Note: The following properties can be used with ::first-letter:

  1. font properties
  2. color properties
  3. background properties
  4. margin properties
  5. padding properties
  6. border properties
  7. text-decoration
  8. vertical-align (only if float is 'none')
  9. text-transform
  10. line-height
  11. float
  12. clear

I think without text-transform you can not do that.....

so try to do that using this code

em {
text-transform:initial;
}
网友答案:
$mystring1 = "<em>this is some</em> dummy text";
$pos1 = stripos($mystring1, '>');
$rest = substr($mystring1, $pos1+1, 1);
$new  = ucfirst($rest);

echo substr_replace($mystring1, $new, $pos1+1, 1);
相关阅读:
Top