Jump to content

[SOLVED] preg_match_all


shawnplr

Recommended Posts

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

/<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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.