Page 1 of 1
elimination of 1st char in string
Posted: Fri Jun 29, 2007 9:45 am
by kanec
i have an array of strings ($ar):
Array (
[0] => (337|260)*gjfg3
[1] => (333|255)ghl07
[2] => (34|34)girghjl07
[3] => 333|255(*gir6l07)
[4] => 353|270(°irl07)
[5] => (34|34)ghettogirl07
)
and i need to make it:
Array (
[0] => 337|260)*gjfg3
[1] => 333|255)ghl07
[2] => 34|34)girghjl07
[3] => 333|255(*gir6l07)
[4] => 353|270(°irl07)
[5] => 34|34)ghettogirl07
)
as you can see the "(" are removed.
i've tried:
f($ar[$i]{0} == "(" ) $ar[$i]= substr($ar[$i] ,1);
but it didn't work. it seems i don't understand the meaning of {0}
i would be glad if someone directed me toward the right path or made me a working version of what i've tried there.
tnx
Posted: Fri Jun 29, 2007 9:48 am
by Gente
it seems i don't understand the meaning of {0}
In this situation me too
Code: Select all
if($ar[$i][0] == "(" ) $ar[$i]= substr($ar[$i] ,1);
Re: elimination of 1st char in string
Posted: Fri Jun 29, 2007 9:54 am
by volka
kanec wrote:i've tried:
f($ar[$i]{0} == "(" ) $ar[$i]= substr($ar[$i] ,1);
but it didn't work. it seems i don't understand the meaning of {0}

It's working fine for me.
Must be something in your surrounding code.
Posted: Fri Jun 29, 2007 9:56 am
by kanec
it's the same as if i used {0}.
it only removes the 1st "(", then when $i++ happens, it does nothing.
Posted: Fri Jun 29, 2007 9:59 am
by Gente
Hmm. Never used {0} style.
Anyway. Post all your script.
Posted: Fri Jun 29, 2007 10:02 am
by kanec
Code: Select all
$ar= explode("\r",$_POST['ar']);
$n= count($ar);
$i=0;
while($i<$n){
echo ($i+1).".) ";
if($ar[$i]{0} == '(') $ar[$i]= substr($ar[$i],1);
echo $ar[$i];
list($addvr_vil_x,$addvr_vil_y,$addvr_res_by)= explode(" ",$ar[$i]);
$i++;
}
this is all the code that would affect that var.
Posted: Fri Jun 29, 2007 10:06 am
by volka
You probably have \r\n as linebrake characters. After explode("\r", ..) the next line will start with a \n character which does not equal (
trim() can remove all those \n chars
Code: Select all
$ar= array_map('trim', explode("\r",$_POST['ar']));
list($addvr_vil_x,$addvr_vil_y,$addvr_res_by)= explode(" ",$ar[$i]);
In you example data the data lines do not contain spaces.
What are you trying to achieve? (I wonder if the answer to that includes the words
browser game 
)
Posted: Fri Jun 29, 2007 10:13 am
by Gente
Code: Select all
<?php
$ar = array (
0 => '(337|260)*gjfg3',
1 => '(333|255)ghl07',
2 => '(34|34)girghjl07',
3 => '333|255(*gir6l07)',
4 => '353|270(°irl07)',
5 => '(34|34)ghettogirl07'
);
$n = count($ar);
$i = 0;
while($i< $n)
{
echo ($i+1).".) ";
if($ar[$i]{0} == '(') $ar[$i]= substr($ar[$i],1);
echo $ar[$i].' ';
$i++;
}
?>
Works perfect. Try to output your array before. Maybe you have troubles with him or with indexes. Also you can change for to foreach (this style is better when you process all the array)
Code: Select all
<?php
foreach($ar as $i => $k)
{
echo ($i+1).".) ";
if($k{0} == '(') $ar[$i]= substr($k,1);
echo $ar[$i].' ';
}
?>
Posted: Fri Jun 29, 2007 10:14 am
by kanec
volka wrote:
What are you trying to achieve? (I wonder if the answer to that includes the words
browser game 
)
obviously
anyway.. thanks for the help. i never thought it may be '\n'.
i just did this:
$ar= explode("\r
\n",$_POST['ar']);
and it works.