elimination of 1st char in string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kanec
Forum Newbie
Posts: 7
Joined: Thu Jun 28, 2007 4:13 am

elimination of 1st char in string

Post 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} :x

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
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

it seems i don't understand the meaning of {0}
In this situation me too :D

Code: Select all

if($ar[$i][0] == "(" ) $ar[$i]= substr($ar[$i] ,1);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: elimination of 1st char in string

Post 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} :x
It's working fine for me.
Must be something in your surrounding code.
kanec
Forum Newbie
Posts: 7
Joined: Thu Jun 28, 2007 4:13 am

Post by kanec »

it's the same as if i used {0}.
it only removes the 1st "(", then when $i++ happens, it does nothing.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Hmm. Never used {0} style.
Anyway. Post all your script.
kanec
Forum Newbie
Posts: 7
Joined: Thu Jun 28, 2007 4:13 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;))
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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].' ';
}
?>
Last edited by Gente on Fri Jun 29, 2007 10:14 am, edited 1 time in total.
kanec
Forum Newbie
Posts: 7
Joined: Thu Jun 28, 2007 4:13 am

Post 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.
Post Reply