0

I am using simple-html-dom for my work. I want to get all PHP script (<?php ... ?>) form file using simple-html-dom.

if i have one file (name: text.php) with below code :

<html>
<head>
    <title>Title</title>
</head>
<body>
    <?php echo "This is test Text"; ?>
</body>
</html>

then how can i get this PHP script <?php echo "This is test Text"; ?> form above file of code using simple-html-dom.

$html = file_get_html('text.php');
foreach($html->find('<?php') as $element) {
    //Sonthing code ...
}

i can not use like this, Is there any other option for this ?

6
  • 1
    don't understand, can you please elaborate more? Commented Jul 26, 2018 at 12:39
  • Won't work with that. Use preg_replace(). Commented Jul 26, 2018 at 12:40
  • SimpleHtmlDOM probably won't make processing instructions visible. PHPs DOM methods could. Better alternative would be the tokenizer (though requires string reconstruction), or a trivial regex. Commented Jul 26, 2018 at 12:44
  • Actually why do you want this ? Commented Jul 26, 2018 at 12:47
  • Can you please tell us more - It's unclear what you are trying to do Commented Jul 26, 2018 at 12:48

1 Answer 1

0

Here's a solution using regex. Note that regex often is not advisable for parsing HTML files. That is, it might be okay in this case.

This will match each instance of a PHP code block and allow you to output (or do whatever else you want) either the entire block (including the tags) or the code that is contained within the block. See the documentation for preg_match_all().

<?php

$string = <<<'NOW'
<html>
<head>
    <title>Title</title>
    <?php echo "something else"; ?>
</head>
<body>
    <?php echo "This is test Text"; ?>
</body>
</html>
NOW;

preg_match_all("/\<\?php (.*) \?\>/", $string, $matches);

foreach($matches[0] as $index => $phpBlock)
{
    echo "Full block: " . $phpBlock;
    echo "\n\n";
    echo "Command: " . $matches[1][$index];
    echo "\n\n";
}

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.