shawnplr Posted February 11, 2007 Share Posted February 11, 2007 Trying to extract part of a link for a city. What I have is preg_match_all('@<a href="[^"]+">(Chicago)</a>@', $page, $matches); What I need is something like $city = Chicago; preg_match_all('@<a href="[^"]+">('.$city.')</a>@', $page, $matches); which obviously doesn't work but I can't seem to figure this one out as simple as it may be. Link to comment https://forums.phpfreaks.com/topic/37971-solved-preg_match_all/ Share on other sites More sharing options...
linuxdream Posted February 11, 2007 Share Posted February 11, 2007 /<A\s*HREF=[\"\']?([^\"\'>]*)[\"\']?[^>]*>($city)<\/A>/i should work since all html tags can have ' " or no quotes Link to comment https://forums.phpfreaks.com/topic/37971-solved-preg_match_all/#findComment-181771 Share on other sites More sharing options...
shawnplr Posted February 11, 2007 Author Share Posted February 11, 2007 /<A\s*HREF=[\"\']?([^\"\'>]*)[\"\']?[^>]*>($city)<\/A>/i Actually that turns out the same as mine. It works when I use Chicago but $city returns no results. Maybe I should have shown more of the code $page = file_get_contents($url); if (preg_match_all('@<a href="[^"]+">(Chicago)</a>@', $page, $matches)) { foreach ($matches[0] as $link) Link to comment https://forums.phpfreaks.com/topic/37971-solved-preg_match_all/#findComment-181804 Share on other sites More sharing options...
shawnplr Posted February 11, 2007 Author Share Posted February 11, 2007 Sorry 'bout that you were right $page = file_get_contents($uri); $city = "Chicago"; $preg = "/<a\s*href=[\"\']?([^\"\'>]*)[\"\']?[^>]*>($city)<\/a>/i"; if (preg_match_all($preg, $page, $matches)) { foreach ($matches[0] as $link) Thank you very much. Link to comment https://forums.phpfreaks.com/topic/37971-solved-preg_match_all/#findComment-181813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.