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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

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

Post 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);

?>
abareplace
Forum Newbie
Posts: 9
Joined: Fri Jan 06, 2012 1:43 am

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

Post 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);
?>
Post Reply