Page 1 of 1

can't find name of URL with regex based on URL

Posted: Wed Mar 14, 2012 8:42 am
by ddragas
hi all

can somebody please help me find regex that gets link name of a URL

I need to get only link name

I've tried these but it doesn't return nothing

Code: Select all

<?php
$fileContent = file_get_contents("http://www.example.com");
$href = "http://www.this-is-my-url.com/and/my/pages/";
$pattern = "/href=\"$href\"/>(.*)(<)/i";
preg_match_all($patern, $fileContent, $match);

print_r($match);

?>

Re: can't find name of URL with regex based on URL

Posted: Wed Mar 14, 2012 10:15 am
by abareplace
1) Use ~ or another delimiter, because you have / in the URL.
2) Remove / after href=\"$href\"
3) Correct $patern to $pattern
4) Use non-greedy repetition to find the next <, not the last < in the file

Code: Select all

<?php
$fileContent = file_get_contents("http://www.example.com");
$href = "http://www.this-is-my-url.com/and/my/pages/";
$pattern = "~href=\"$href\">(.*?)<~i";
preg_match_all($pattern, $fileContent, $match);

print_r($match);
?>