I have the following string below which was extracted by DOM Document
:
$scripts = $doc->getElementsByTagName('script');
$script = $scripts->item($i);
$string = $script->getAttribute('src');// Save the string 'jquery-3.2.0.min.js'
I have 3 ways to check if this string is a jquery:
jquery(?:\\-|\\.)([\\d.]*\\d)[^/]*\\.js\\;version:\\1
/([\\d.]+)/jquery(?:\\.min)?\\.js\\;version:\\1
jquery.*\\.js
In that case how can I validate the above string using one of these 3 commands?
Use preg_match
preg_match — Perform a regular expression match
if (preg_match("/jquery.*\.js/",$string)) {
echo "Match found";
}
If it finds a match it will print out Match found
. (I just used one of your regex strings)
I'm also assuming that $string
is what you extracted, because it was hard to tell.
You can easily select script tags that are only links to the jquery lib using xpath:
$xp = new DOMXPath($doc);
$nodeList = $xp->query('//script[.=""]/@src[contains(., "jquery")]');
foreach ($nodeList as $node) {
echo $node->nodeValue, PHP_EOL;
}