You are here: Re: [PHP] [HAB] str_replace « PHP « IT news, forums, messages
Re: [PHP] [HAB] str_replace

Posted by Bret Hughes on 02/22/05 18:58

On Tue, 2005-02-22 at 09:59, OOzy Pal wrote:
> Dears
>
> I am trying replace anything between '{!' and '!}' i.e {!A!} and {!B!}
> and {!ABC!} with XYZ. I tried the following but no help. Can anyone
> help?
>
> $this->template=str_replace("{!*!}","XYZ",$this->template);
>
> Thank you
>
>

IIRC str_replace does not use any type of globbing. You need to use one
of the function that uses regular expresions I prefer preg_replace
personally. regexes can get complicated looking pretty quick but are an
incredibly powerful mechanism once you understand even the rudiments. I
would place my self in that catagory.

try something like :

$this->template = preg_replace ("/![^!]*!/","!XYZ!",$this->template);

untested but should be close.

what the /![^!]*!/ says is match any string that starts with a ! and is
followed by zero or more characters that are not ! followed by a ! the
replacement part of the function replaces it all with !XYZ!.

this will also catch the string !! and replace it with !XYZ! if this is
not what you want replace the * above with + (one or more) exactly one
char between them leave the * off altogether.

HTH

Bret

 

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

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