You are here: Re: Change alternate commas to semi colons... « PHP Programming Language « IT news, forums, messages
Re: Change alternate commas to semi colons...

Posted by Rik on 06/20/06 19:36

StevePBurgess@gmail.com wrote:
> With a string of authors such as:
>
> Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
> Shawyer, Andrea
>
> I would like to write a function to change every other comma into a
> semi colon (thereby defining where one name ends and the next begins).
>
> I could do it by writing a complex (and slow) procedure - but is there
> a quick way of doing it using regular expressions, for example?


Regular expressions can be used, I'd think this is faster:

$array = explode(',', $authors);
$new_authors = '';
$i = 0;
while(!empty($array)){
$delimter = (($i % 2)==0) ? ',' : ';';
$new_authors .= array_shift($array).$delimiter;
$i++;
//or, to get an array:
//$new_authors[] = array_shift($array).','.array_shift($array);
}


If you want a regular expression (which almost certainly will be slower):
$authors = preg_replace('/([^,]*),([^,]*),/s','$1,$2;',$authors);

Similar, to get them directly in an array:
preg_match_all('/([^,]*,){2}/s',$authors,$matches);


Grtz,
--
Rik Wasmus

 

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

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