0

i need to extract a value (id value) from html web page . this value
is included in JavaScript code .

<script type="text/JavaScript">
var geoInstance = gioFinder("geo");
geoInstance.setup({
    id: "126568949", // i need this
    geometre: "triangle",
    type: "html",
    image: 'https://example/126568949.jpg',
});
</script>

my code php is

    <?php
    $filename = "https://examlpe.com";
    $handle = fopen($filename, "r");
    if ($handle) {
        while (!feof($handle)) {
            $text. = fread($handle, 128);
        }
        fclose($handle);
    }

    // How can you do

//right answer is . 
$result = preg_match('/(?<=id: ")(.*)(?=",)/', $text, $matches);

echo $matches[0];
//end

    ?>
6
  • 1
    Essentially what you are asking is how to write a text parser for the specific case above. Where is your attempts at this? Not just opening a file and reading it block by block. Where is your code that does the parsing? Commented Jan 30, 2020 at 18:31
  • 1
    What exactly do you know about your “needle”? That it’s always a 9-digit number? That its always the id property in this geoInstance parameter? This question could use a bit of clarity. Commented Jan 30, 2020 at 20:23
  • 1
    Please do not use block quotes to highlight paragraphs. Look at posts from experienced users, and copy their style - people will spend more time on your question if you format it properly. Commented Jan 30, 2020 at 20:45
  • 1
    Since you've implemented the code from my answer, is it working now or are you still having issues? Commented Jan 30, 2020 at 21:39
  • no ! it working Commented Jan 30, 2020 at 22:10

1 Answer 1

2

You could read the file as a string and then use RegEx to find the specific value.

An example for your case could be the RegEx pattern (?<=id: ")(.*)(?=",).

Then use preg_match: preg_match('(?<=id: ")(.*)(?=",)', $html);

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

1 Comment

Correct the answer $result = preg_match('/(?<=id: ")(.*)(?=",)/', $text, $matches);

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.