On Tue, Nov 18, 2003 at 04:37:52PM +1100, Martin Towell wrote:
: 
: I have an array of strings in the following format:
:       "abcd - rst"
:       "abcd - uvw"
:       "abcd - xyz"
:       "foobar - rst"
:       "blah - rst"
:       "googol - uvw"
: 
: What I want to do is strip everything from the " - " bit of the string to
: the end, _but_ only for the strings that don't start with "abcd"
: 
: I was thinking something like the following:
:       echo ereg_replace("(!abcd) - xyz", "\\1", $str)."\n";
: but obviously this doesn't work, otherwise I wouldn't be emailing the
: list...

You can't use "!" because it's not a valid special character in regular
expressions.  It's really hard to craft do-not-match-this-word patterns.
You're better off separating the two pieces of logic.

        $arr = array(
            "abcd - rst",
            "abcd - uvw",
            "abcd - xyz",
            "foobar - rst",
            "blah - rst",
            "googol - uvw"
        );

        reset($arr);
        while (list($key, $value) = each($arr))
        {
            if (substr($value, 0, 5) != 'abcd ')
            {
                $arr[$key] = ereg_replace('^(.*) - .*$', '\1', $value);
            }
        }

        print_r($arr);

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

Reply via email to