You are here: Re: [PHP] Replacing 2 strings « PHP « IT news, forums, messages
Re: [PHP] Replacing 2 strings

Posted by W Luke on 10/21/20 11:17

On 31/05/05, Murray @ PlanetThoughtful <lists@planetthoughtful.org> 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 -

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация