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 Juliette on 06/20/06 19:41

bgeneto wrote:
> 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?
>>
>> Many thanks.
>
> Why not just use str_replace() intrinsic function?
>
> $str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
> Karen, Shawyer, Andrea";
> $str = str_replace(",", ";", $str);
> echo $str;
>
> Bernhard Enders.
>

Because he wants to change every *other* comma into a semi-colon (i.e.
change the second, fourth etc).
Your function would change *every* comma into a semi-colon.

I don't think you can easily do it with regex as you can't really
differentiate between comma's.
I would suggest something along the lines of:

$array = explode ( ', ', $string );
$arraycount = count($array);
$new_string = '';

for( $i=0; $i<$arraycount; $i++; ) {
if ((1&$i)) {
//$i is odd
$new_string .= $array[$i] . ', ';
}
elseif (!(1&$num)) {
//$i is even
$new_string .= $array[$i] . '; ';
}
}
$string = $new_string;
unset($new_string);

Hope this helps,
Juliette

 

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

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