samedi 23 novembre 2019

How to print a variable contains double quotation in PHP after usage of htmlspecialchars

I'm trying to get the value after == in the if condition, which make the condition TRUE. I have the following example:

(code.php)

<?php

$a = 'Data';
if(substr($a, 0, strlen("<SCRIPT"))== "<SCRIPT" ) {
   echo TRUE;
}
?>

In the above example, I'm trying to get the value <SCRIPT without double quotations. I tried some steps, but it still get the double quotation with the value ("<SCRIPT"). Next code shows my code to get the result:

(test.php)

<?php
$file = file_get_contents("code.php"); // Code.php the page that include the if condition 
$lines = explode("\n", $file); // get each line of source code and store it in array ($lines)

function sanitize_recursive($s) {
    if (is_array($s)) {
      return(array_map('sanitize_recursive', $s));
    } else {
      return htmlspecialchars($s);
    }
}

foreach ($lines as $key => &$value) {
    if(strpos($value, 'if') != false) // check if the line have if statement 
    {
        if((strpos($value, '==')) !== false )  // check if the line compare two values
        {

            $pos1 =  strpos($value, '==') + 2; // get the existence position of '==' + 2 

            $pos2 =  strrpos($value, ')'); // get the position of last ) in the line

            $startIndex = min($pos1, $pos2);
            $length = abs($pos1 - $pos2);

            $between = sanitize_recursive(substr($value, $startIndex, $length)); // get the value between 2 position + sanitize the tag to show it

            echo $between; // will print: "<SCRIPT" with double quotation
        }
    }
}
?>

I want a way to print the value that make the if condition be TRUE, which is <SCRIPT without double quotation. Is there a way to check the first character in $between variable, and remove it.

Note: I know the condition here is not TRUE, so that I want to get the value that make the condition TRUE which is <SCRIPT Note: I tried $between[0], but it get & which is from &quot

Note: The purpose is I want to know what is the value that make the condition TRUE, then later will use this value to do test cases on the code based on this value.

Thank you

Aucun commentaire:

Enregistrer un commentaire