Re: ereg_replace help

From: Date: Tue, 18 Nov 2003 08:02:13 +0000
Subject: Re: ereg_replace help
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-170071@lists.php.net to get a copy of this message
On Tue, 2003-11-18 at 02:30, Jason Wong wrote: > On Tuesday 18 November 2003 13:37, 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... > > > > Can anyone help? I need to use ereg_replace() because it's part of our code > > library and therefore can't change :( > > May be quicker (definitely easier) to explode() on ' - '. The following is untested: ----------------------------------------------------------- foreach( $myArray as $id => $entry ) { if( ($pos = strpos( 'abcd' ) !== false && $pos === 0 ) { $parts = explode( ' - ', $entry ); $myArray[$id] = $parts[0]; } } print_r( $myArray ); ----------------------------------------------------------- Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#170071) next »