Re: A little regex help
| From: | Justin French | Date: | Sun, 15 Dec 2002 01:04:28 +0000 |
| Subject: | Re: A little regex help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-128310@lists.php.net to get a copy of this message | ||
Not sure if it will be quicker or not, but I don't think you NEED a regex:
- get rid of 'for more information'
- split on 'See' into $pre and $code
- trim the bits down
- wrap $code in a URL
- glue it all back together
UNTESTED CODE:
<?
// string
$str = 'Windows 2000 Hotfix (Pre-SP4) See Q322842 for more information';
// set up
$ms = "http://support.microsoft.com/default.aspx?scid=kb;en-us;";
$suffix = 'for more information';
$splitter = 'See';
// do it
list($pre,$code) = explode($splitter,$str = str_replace($suffix,'',$str));
$pre = trim($pre); $code = trim($code);
$str = "{$pre} See <a href=\"{$ms}{$code}\">{$code}</a> {$suffix}";
echo $str;
?>
If this gets heavy traffic, u may want to test which is faster -- or you may
not care!!
Justin
on 14/12/02 5:21 AM, Gareth Hastings (gareth@netgs.co.uk) wrote:
> Hi,
>
> I'm trying to work out the regex needed to split either one of the
> following lines
>
> Windows 2000 Hotfix (Pre-SP4) See Q322842 for more information
> Windows XP Hotfix (SP2) See Q327696 for more information
>
> Into one of these
>
> Windows 2000 Hotfix (Pre-SP4) See <a
>
> href='http://support.microsoft.com/default.aspx?scid=kb;en-us;Q322842'>Q
> 322842</a> for more information
> Windows XP Hotfix (SP2) See <a
>
> href='http://support.microsoft.com/default.aspx?scid=kb;en-us;Q327696>Q3
> 27696</a> for more information
>
> I can work out half of the expression but not the other half lol, I've
> used a combination of ereg and split but I'm sure this can be done with
> 1 ereg statement. Here is my code
>
> $ms =
> "http://support.microsoft.com/default.aspx?scid=kb;en-us;";
>
>
> if (ereg("(\[See )([Q|q][0-9]+)( for more information\])", $name))
> {
> $sp = split("\[See ", $name);
> ereg("([Q|q][0-9]+)( for more information\])", $sp[1], $qb);
>
> $url = $sp[0] . "See <a href='$ms" . $qb[1] . "'
> target='_blank'>" . $qb[1] . "</a> for more information";
>
> }
>
> Any ideas?
>
> Gareth
>
>
Justin French
--------------------
http://Indent.com.au
Web Development &
Graphic Design
--------------------