Search code examples
phpcountstrlen

PHP Count characters inside title tag


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}}

Solution

  • $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