Re: preg_replace() help

From: Date: Fri, 13 Jul 2007 21:03:28 +0000
Subject: Re: preg_replace() help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258819@lists.php.net to get a copy of this message
Rick Pasotto wrote:
I have quotes like the following: $txt = 'A promise is a debt. -- Irish Proverb'; I'd like to replace all the spaces afer the '--' with   This is what I've tried: $pat = '/( --.*)(\s|\n)/U';
    $rpl = '$1$2 ';
    while (preg_match($pat,$txt,$matches) > 0) {
        print "$txt\n";
        printf("[0]: <%s>\n",$matches[0]);
        printf("[1]: <%s>\n",$matches[1]);
        printf("[2]: <%s>\n",$matches[2]);
        preg_replace($pat,$rpl,$txt);
    }
The prints are for debugging. $matches contains what I expect but nothing gets replaced and $txt stays the same so it loops forever. What am I doing wrong?
Maybe this <?php $txt = 'A promise is a debt. -- Irish Proverb'; $parts = explode('--', $txt, 2); $parts[1] = str_replace(' ', '&nbsp;', $parts[1]); echo join('--', $parts); ?> -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#258819) next »