Jump to content

Archived

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

arfa

regex help - plz

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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: 1149763623
LINK TEXT: link text

Array
(
    [0] => &href=1149763623&q=1>link text
    [1] => 1149763623
    [2] => link text
)[/code]

Share this post


Link to post
Share on other sites
many thanks for you replies

both 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 thanks
arfa

Share this post


Link to post
Share on other sites

×

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.