Re: ereg_replace help
| From: | Eugene Lee | Date: | Tue, 18 Nov 2003 07:22:01 +0000 |
| Subject: | Re: ereg_replace help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-170063@lists.php.net to get a copy of this message | ||
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);