snarkyazoid
snarkyazoid

Reputation: 455

How to use token_get_all to find all T_INLINE_HTML?

I recently heard about a function called token_get_all and I want to find out how to get all of the T_INLINE_HTML out of a php file containing PHP code and HTML. For example, a file like this:

<?php
echo "This is my PHP code";
?>
<html>
<p>Hello, this is the HTML code I want from token_get_all!</p>
</html>

I was attempting to use:

$tokens = token_get_all($filecontents);
$html = $tokens(T_INLINE_HTML);

but that didn't work. How might I do this?

Upvotes: 1

Views: 825

Answers (1)

user142162
user142162

Reputation:

The following code will filter out the tokens of a file which and leave you with only inline HTML ones:

$tokens = token_get_all($filecontents);
$tokens = array_filter($tokens, function($token)
{
   return $token[0] == T_INLINE_HTML;
});

Upvotes: 5

Related Questions