Re: Replacing 2 strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 31/05/05, Murray @ PlanetThoughtful <lists@xxxxxxxxxxxxxxxxxxxx> wrote:
> > <?
> >
> > function replace($string){
> >       preg_match("/^<\^([a-zA-Z]+?)_([a-zA-Z]+?)>/", $string, $matcharr);
> >       $string = str_replace($matcharr[0], $matcharr[1] . " " .$matcharr[2]
> > . ":", $string);
> >       return $string;
> >
> > }
> >
> > $string = "<^JIM_JONES> Leicester, 1720.  Oxford, 1800 CONFIRMED: meeting
> > at
> > 19.10";
> > echo replace($string);
> >
> > ?>
> >
> > One of the small benefits of this solution is that Will doesn't need to
> > know
> > what is contained in the target substring beforehand.
> 
> I should get into the habit of listing the assumptions my code makes.
> 
> In the above example, the following assumptions are present:
> 
> - The target substring (in this example, "<^JIM_JONES>") must *always*
> appear at the beginning of the string for the function to perform its task
> 
> - The target substring will *always* begin with "<^", will feature a "_"
> between the two name elements, and will conclude with ">"
> 
> - There will only be two name elements within the target substring (ie will
> match "<^JIM_JONES>" and "<^MARY_BETH>" but will not match "<^JIM>" or
> "<^MARY_BETH_JONES>")
> 
> - The function will only replace the first incidence of the target
> substring. In the eventuality that the target substring value appears
> multiple times in the string being processed, all other instances will be
> left unchanged.
> 
> - All other contents of the string being processed can vary without impact
> on the function.

Thanks Murray, and Brian - both excellent and I'm really grateful for
the help!  Clueless when it comes to these types of problems, so
thanks very much,

-- 
Will   The Corridor of Uncertainty   http://www.cricket.mailliw.com/
 - Sanity is a madness put to good use -

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux