Regular expression to get internal links only

Clash Royale CLAN TAG#URR8PPPRegular expression to get internal links only
I am trying to get internal links only with the following code.
$domain="http://www.nydailynews.com";
$data = file_get_contents($domain);
$content=preg_match( '(.*'.$domain.'.*)|(^/.*$)', $domain, $matches );
but getting this error
Warning: preg_match(): Unknown modifier '|' in C:xampphtdocscrawlcrawlindex.php on line 4
what should be the regular expression for that so I can get sub pages of a website?
/(.*' . $domain . '.*)|(^/.*$)/
parse_url()
@Heyne added that, but now it says Warning: preg_match(): Unknown modifier '/'
– e2e
55 mins ago
This is because your
$domain variable holds a string with the delimiter '/' so you need to escape it like this: $content=preg_match( '/(.*'.preg_quote($domain, '/').'.*)|(^/.*$)/', $domain, $matches );– Karol Samborski
7 mins ago
$domain
$content=preg_match( '/(.*'.preg_quote($domain, '/').'.*)|(^/.*$)/', $domain, $matches );
1 Answer
1
Just try this, it works
<?PHP
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
$url = "http://www.nydailynews.com";
$input = file_get_contents($url) or die("Could not access file: $url");
$regexp = "<as[^>]*href=("??)([^" >]*?)\1[^>]*>(.*)</a>";
if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
echo $match[2]."<br/>";
}
}
?>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You need to add delimiters around your regex like this:
/(.*' . $domain . '.*)|(^/.*$)/. What do you mean with "sub-pages" of a website? The path? Take a look atparse_url()then.– Heyne
1 hour ago