arfa Posted June 9, 2006 Share Posted June 9, 2006 I think this is pretty simple but my regex study is still very new.I am walking through a flatfile database trying to pull two values out. A typical line reads:<LI><!zx1><a class=cal href=events.php?act=tit&da=4&mo=6&yr=2006&href=1149763623&q=1>link text</a>I am after the number value &href= [eg. 1149763623] and the *link text*. There are various variations on this standard line so I need to work between the <LI> and the </a>I have (frustratingly) tried a huge range of permutations starting with something like... preg_match_all("#<LI>?????>(.+?)????</a>#", $line, $matches);$numbers=$line[1];preg_match_all("#<LI>?????>(.+?)</a>#", $line, $matches);$link_text=$line[1];I hope to find the bits to replace the ???ANY suggestions will be much appreciated.thanks - arfa Quote Link to comment https://forums.phpfreaks.com/topic/11562-regex-help-plz/ Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 regex for getting the number would be something like /[^<]+href=([^&]+)/ Just var_dump($matches) and you can see where your number will be. As for the link text: /<a[^>]*>(.*?)</a>/s Quote Link to comment https://forums.phpfreaks.com/topic/11562-regex-help-plz/#findComment-43679 Share on other sites More sharing options...
poirot Posted June 9, 2006 Share Posted June 9, 2006 You can always use a simple regex like:[code]<?php$str = '<LI><!zx1><a class=cal href=events.php?act=tit&da=4&mo=6&yr=2006&href=1149763623&q=1>link text</a>';preg_match("/&href=([^&]+)(?:[^>]*)>([^>]*)<\/a>/", $str, $m);echo '<pre>';echo 'HREF: ' . $m[1] . "\n";echo 'LINK TEXT: ' . $m[2] . "\n\n";print_r($m);?>[/code]Which generates:[code]HREF: 1149763623LINK TEXT: link textArray( [0] => &href=1149763623&q=1>link text [1] => 1149763623 [2] => link text)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11562-regex-help-plz/#findComment-43716 Share on other sites More sharing options...
arfa Posted June 10, 2006 Author Share Posted June 10, 2006 many thanks for you repliesboth solutions work but...the line provided is only typical and there are variations.there needs to be allowance for various other text/data/lnks prior to and after the <LI>....</a>I am trying various .?*+ permutations but this is all part of my learning curve so further guidance would be much appreciated.Progress is being made - many thanksarfa Quote Link to comment https://forums.phpfreaks.com/topic/11562-regex-help-plz/#findComment-43964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.