Ok I may actually have an answer here...

<?

$str = 'hubble, bubble, toil, trouble';

function andificate($str)
{
        return preg_replace('/([\s]?,[\s]?)(\w+)$/', ' and \\2', $str);
}

echo andificate($str);

?>

Gives:

hubble, bubble, toil and trouble

Accepts spaces before and/or after commas or neither... preserves spaces

Hope this one works (and that I've read the post right :-/)

Dan
--

-----Original Message-----
From: Richard Lynch [mailto:[EMAIL PROTECTED] 
Sent: 03 February 2006 23:34
To: Barry
Cc: php-general@lists.php.net
Subject: Re: [PHP] Regular expression

$last_comma = strrpos($string, ",");
$string = substr($string, 0, $last_comma) . ' and ' . substr($string,
$last_comma);

I probably have a one-off error in there somewhere, or swapped the
order of args in strrpos, but you get the idea.
http://php.net/strrpos

On Mon, January 30, 2006 8:09 am, Barry wrote:
> Simple reg help please
>
> i want to match the last "," in "a,b,c,d" and replace it with " and "
>
> i tried ereg_replace(",([a-zA-z])*$"," and ",$string);
>
> but i forgot how to add the "d" which is also matched now back to the
> " and "
>
> Can you give any good reg_exp sites where to learn it?
> Its long ago since i used reg exp and i lost the hang of it... :(
>
> btw. any sites that have reg_exp that works witht PHP would be fine.
> i know http://www.regular-expressions.info/tutorial.html
> But that examples dont work with preg_match and ereg.
>
> Thanks ^_^
>
> --
> Smileys rule (cX.x)C --o(^_^o)
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


__________ NOD32 1.1393 (20060203) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to