Abul Hasnat
Abul Hasnat

Reputation: 1631

php regex/preg_match for beginner

I am not good at expressions I would like to match the string below of a string.

http://www.site.com/ * .js

preg_match('(http://www\.site\.com/).*(\.js)',$html,$match);

I know this code is not right. * Represents any file with .js extension. Could anyone guide me with the expression. Sorry if any duplication.

Upvotes: 0

Views: 428

Answers (1)

Anthony Simmon
Anthony Simmon

Reputation: 1607

You have to use delimiters such as '#', '@' or '/' in the pattern :

$url = 'http://www.site.com/javascript/test.js';

$preg_match = preg_match('#(http://www\.site\.com/)(.*)(\.js)#', $url, $matches);

if($preg_match === 1)
{
    var_dump($matches);
    // displays :
    // array
    //   0 => string 'http://www.site.com/javascript/test.js' (length=38)
    //   1 => string 'http://www.site.com/' (length=20)
    //   2 => string 'javascript/test' (length=15)
    //   3 => string '.js' (length=3)
}
else
{
    // doesn't match
}

Upvotes: 1

Related Questions