This is my code, it will echo "Not working"
$f = file_get_contents("http://www.google.com");
$text = htmlspecialchars( $f );
$matches = array();
preg_match('@<a.*?</a>@s', $text, $matches);
if ($matches) {
$text2 = $matches[0];
echo $text2;
}
else {
echo "Not working";
}
If I made a variable:
$text = '<a href="http://www.google.com">Google</a> is your best friend!';
This will work somehow, but it wont when I take it from the:
$text = htmlspecialchars( $f );
Anyone knows why?
This is because htmlspecialchars
translates all special characters <&>"'
, etc into html entities (e.g., &
becomes &
). Thus your match fails.