Jump to content

regex help - plz


arfa

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.