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. |
`------------------------------------------------------------'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php