Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

removing every even character from a string

0 views
Skip to first unread message

yawnmoth

unread,
Oct 9, 2007, 2:14:57 AM10/9/07
to
Say I had .a.b.c and wanted to return abc. Any ideas as to how I'd do
this? Here's my attempt, but it doesn't work:

<?php
echo preg_replace('#(?:.(.))+#s', '$1', '.a.b.c');
?>

Any ideas?

Vladimir Ghetau

unread,
Oct 9, 2007, 4:14:51 AM10/9/07
to
Yawnmoth,

Try focusing on the simple rules:

<?php

echo preg_replace('/[^A-Z]/i', NULL, '.a.b.c');

?>

Ouputs just what you need.

Best luck!

Vladimir
http://www.Vladimirated.com

Ron Barnett

unread,
Oct 9, 2007, 4:14:48 AM10/9/07
to
"yawnmoth" <terr...@yahoo.com> wrote in message
news:1191910497.2...@50g2000hsm.googlegroups.com...

Hi, If you know that the characters are dots, just str_replace them with
nothing

$new = str_replace('.','','.a.b.c');

or if they are every other character , regardless of value, try:

$string = '.d.fjdghdjtgkfdhfr';
$new = '';
for ($n = 2 to len($string)+1){
if ($n%2==0) $new .= substr($string,$n-2,1);
}

this will remove all the odd characters,
testing for $n modulus 2 ==1 will take out the even characters

HTH

Ron


ZeldorBlat

unread,
Oct 9, 2007, 9:23:56 AM10/9/07
to
On Oct 9, 4:14 am, "Ron Barnett" <N...@Spam.Thanks> wrote:
>
> or if they are every other character , regardless of value, try:
>
> $string = '.d.fjdghdjtgkfdhfr';
> $new = '';
> for ($n = 2 to len($string)+1){
> if ($n%2==0) $new .= substr($string,$n-2,1);
>
> }
>
> this will remove all the odd characters,
> testing for $n modulus 2 ==1 will take out the even characters
>
> HTH
>
> Ron

Or, even simpler:

$new = '';
for($i = 1; $i < len($string); $i += 2)
$new .= $string[$i];

0 new messages