2

UPDATED:

I am using RecursiveIteratorIterator to scan the directories to read index.html file.

Now, based on the Pubname and Pubversion, i am trying to read the corresponding index.html file, to get the values of Readability Grade, Readability Score etc. (by storing the values in local storage)

The values are fetched from local storage if i use the html and JS code separately, but after i integrate into php, it is not working.

Code:

<?php
//error_reporting(0);

//Get Pub version
function get_version($path){
    $needle = "=";
    if(($pos = strpos($path, $needle)) != 0){
         $bits = str_split($path, $pos + strlen($needle));

         $integer_count = 0;
         for($x = 0; $x < strlen($bits[1]); $x++){
             if(is_numeric($bits[1][$x])){
                 $integer_count++;
             }else{
                 break;
             }
         }
         if($integer_count > 0){
             $bits = str_split($bits[1], $integer_count);
            return  $bits[0];
         }
    }
    return -1;
}


$it = new RecursiveDirectoryIterator("C:\Users\Sachin_S2\Desktop\Script");

foreach(new RecursiveIteratorIterator($it,RecursiveIteratorIterator::SELF_FIRST) as $file) {

    //IGNORE FILE TYPES
    //$filetypes = array("jpg", "png", "pdf", "css", "csv","log","txt");
    $htmlfiles = array("html");
    $filetype = pathinfo($file, PATHINFO_EXTENSION);
    if (in_array(strtolower($filetype), $htmlfiles)) {

        // skip dot files while iterating
        $it->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);

        $files = array($file);
        $pathname = $file->getPathname();
        print_r($pathname);

        $version = get_version($pathname);
        if($version > 0){
            // use $version to read correct file
            include 'html/home.html';
        }
        echo '*********************************************************************************'.'<br/><br/>';

    }       
}

html/home.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>SEO Metrics</title>
</head>

<body>
        <!--Begin script code-->
        <form name="myform3">
            <!-- 
            <input type="hidden" name="formvar" value="">
            <input type="file" name="file" id="file">
            <p id="demo"></p><br/><br/>
            -->

            <div class="form-group">
              <label>Readability Grade:</label>
              <input type="text" class="form-control" id="grade">
            </div>
            <div class="form-group">
              <label>Readability Score:</label>
              <input type="text" class="form-control" id="score">
            </div>
            <div class="form-group">
              <label>Total Word Count:</label>
              <input type="text" class="form-control" id="words">
            </div>
        </form>
        <!--END script code-->

      <!--Custom JS code-->
      <script src="js/home.js"></script>
</body>
</html>

js/home.js

$(document).ready(function() {


document.getElementById('file').onchange = function() {

    const file = this.files[0];
    const reader = new FileReader();

    reader.onload = (event) => {
      const file = event.target.result;
      const allLines = file.split(/\r\n|\n/);
      let arr = allLines;


      arr.forEach((kv) => {
        if (kv.includes("Flesh-Kincaid Grade Level")) { 
                var fetch_grade = kv.replace(/<\/?[^>]+(>|$)/g, "");
                var formated_grade = fetch_grade.split(":").pop();          //Remove part of the string before ":"
                localStorage.setItem("Readability_Grade", formated_grade);

                document.getElementById('grade').value = localStorage.getItem("Readability_Grade").replace(/\s/g, "");  //Assign localStorage to text box
                localStorage["Readability_Grade"] = grade.value;
                //alert(formatedgrade);
        }
        if (kv.includes("Flesh Reading Ease Score")) { 
                var fetch_score = kv.replace(/<\/?[^>]+(>|$)/g, ""); 
                var formated_score = fetch_score.split(":").pop();
                localStorage.setItem('Readability_Score', formated_score);
                document.getElementById('score').value = localStorage.getItem("Readability_Score").replace(/\s/g, "");
                //alert(formatedscore);
        }
        if (kv.includes("Reuse Metrics Score")) { 
                var metricscore = kv; 
                //alert(metricscore);
        }
        if (kv.includes("Total words")) { 
                var totalwords = kv.replace(/<\/?[^>]+(>|$)/g, "");
                var total_words_formated = totalwords.split(":").pop();
                localStorage.setItem('Word_Count', total_words_formated);
                document.getElementById('words').value = localStorage.getItem("Word_Count").replace(/\s/g, ""); 
                //alert(totalwords);
        } 
     }); 
  };

    reader.onerror = (event) => {
      alert(event.target.error.name);
    };

    reader.readAsText(file);
  };

});

Output:

On reading this php script, i should be able to read index html (for each file paths - based on pubname and version), and get the values of Readability_Grade and score etc., which i am not able to get.

enter image description here

7
  • 1
    “I am not able to read index.html file based on the pubname and pubversion” - why, what is the actual problem? “I am not able to” is not helpful as a problem description. Commented Aug 6, 2019 at 12:28
  • Try edit you question to be a bit more clear, it's not clear what you're asking! Commented Aug 6, 2019 at 12:29
  • I have edited the question @misorude. Hope the question is clear now? Please help on this Commented Aug 6, 2019 at 12:54
  • No, still unclear. I have no idea what you mean by “how to search or split the path name”. Not even clear what exactly is supposed to be “input” and what “output” here. Do you have the pubname and pubversion upfront, and now you want to search for the corresponding index file? So basically all you need to do is check whether the current folder name starts with ESXi_6.7_GSG_Pub=9? Commented Aug 6, 2019 at 13:04
  • I dont get what else information is needed. I just need to read html file corresponding to the Pubname and Pubversion. For xyz pubname and 123 pubversion, read the html file....for abc pubname and 456 pubversion, read the html file. I jus need help in how to get the Pubname and version from the list of multiple file paths and read its corresponding html file in that folder . Commented Aug 6, 2019 at 13:08

1 Answer 1

1

Firstly get the version number from the string, below is a method to do just that.

function get_version($path){
    $needle = "Pub=";
    if(($pos = strpos($path, $needle)) != 0){
         $bits = str_split($path, $pos + strlen($needle));

         $integer_count = 0;
         for($x = 0; $x < strlen($bits[1]); $x++){
             if(is_numeric($bits[1][$x])){
                 $integer_count++;
             }else{
                 break;
             }
         }
         if($integer_count > 0){
             $bits = str_split($bits[1], $integer_count);
            return  $bits[0];
         }
    }
    return -1;
}

And then you can use it like this.

foreach ($paths as $path){
    $version = get_version($path);
    if($version > 0){
        // use $version to read correct file
    }
}

Thats if I undertoood your question.

Disclaimer: Quickly typed up but should work.

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

5 Comments

I have modified the code as per your code @akaBase, now i need to fetch the values of few parameters (Readability_Grade, Score,..) from index.html. Can you please help on this?
Thats a different Question I will take a look at the new question once you have posted it, and if this has answered your question can you accept it,
Accepted, but the question title and code is related, hence updated here. can i post the same code with different question? Will the 2 links not be duplicate!
No shouldn't be duplicates if I'm understanding you, this one is getting a value from a path and the new one is getting a value from html?
I have created a new question here : stackoverflow.com/questions/57394548/…

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.