php - CURL and Pregmatch a specefic file address
Get the solution ↓↓↓I am trying to curl a URL and the result is this :
Curl command in PHP :
$url = "https://example.com/api/v1/report_creator/generate.pdf";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_exec($ch);
curl_close($ch);
Output :
<script type="text/javascript">
$(function() {
var timeout = 1000;
var checkReport = function() {
$.getJSON("b15a02aa-182d.json")
.fail(function() {
setTimeout(checkReport, timeout);
});
};
checkReport();
});
</script>
Now i need to fin the json file : b15a02aa-182d.json
So I tried to do :
preg_match_all("/$.getJSON(\"(.*)\")/", $result, $matches);
print_r($matches);
But wasn't able to extract the json address
Answer
Solution:
You've neglected to escape the special characters in the Regex pattern. Certain characters do specific things in Regex. From your pattern there's:
$
which matches the end of the string.
which matches any character, used correctly in your capture group(
and)
which denote a group, again used correctly elsewhere
These characters all need to be escaped to match the actual character, rather than do the special thing they represent. Escape them the same way you did with the"
character and it works.
preg_match_all("/\$\.getJSON\(\"(.*)\"\)/", $result, $matches);
print_r($matches);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.