PHP计数标题内的字符

I'm trying to count the text inside my title tag. But it doesn't seem to work. I've tried count(), strlen() etc. For example:

<title>Count me!</title>

When i use strlen() i get this error:

strlen() expects parameter 1 to be string, object given

When i use count u just get 2. I think it counts the title tags inside my file.

Controller:

$title = $dom->getElementsByTagName('title');
$count = strlen($title);

Index:

{{$count}}
$title_tag = $dom->getElementsByTagName('title');
foreach($tital_tag as $title){
$title_length = strlen($title->nodeValue);
}

You need to loop through title tags ... It doesn't mean you have multiple tags . Dom simply return object which need to be accessed and then you get length of title

According to the documentation, DOMDocument::getElementsByTagName returns a DOMNodeList object, not a string.

You need to iterate over the elements in the list with a foreach loop and retrieve the length of each node's value.

Just an alternative using jquery you can count the character as follows :

<script type="text/javascript">
$(document).ready(function(){
    var title = $(document).prop('title').length; 
    console.log(title);
});

</script> 

You can't achieve the result doing just that because what the getElementsByTagName() function does is return "A NodeList object, representing a collection of elements with the specified tag name.".

ie. if you called it on a ul tag it would give you a collection of li tags within that ul.

You could try this though;

<html>
<head>
    <title>Heello</title>
</head>
<body>
    <script type="text/javascript">
        var text = document.getElementsByTagName("title")[0].innerHTML.length;//get the length of the first title tag
        alert(text);//text no contains the length of characters within the title tag
    </script>
</body>
</html>

Note: The elements in the returned collection are sorted as they appear in the source code.

</div>