$char   = '3';
$string = '101131110|101131110|';

$positions = array();
$pos = -1;
while (($pos = strpos($string, $char, $pos+1)) !== false) {
    $positions[] = $pos+1; 
}

$result = implode(',', $positions);
print_r($result);

The position of '3' in our string was 5,15
I want to get a second position of '3' in position 14 instead of 15, I had to pass this character "|"
anybody help me. thanks.

regards,
ivanichi

don't use string position function. use explode function.
this will count all the characters including special cahracters on the type char, string, double, float and int.
so in this case the syntax should be.

$array_of_str=explode('',$string);
//converts the $string into an array element per character
print_r($array_of_str)
$i=0;
foreach($array_of_str as $array_of_chars){
//read the position of the preferred character
if($array_of_chars[i]==$char){
echo " the position of 3 is in position number ".$i;
}
$i++;
}

if use this syntax

 $array_of_str=explode('|',$string); 
 //converts the $string into an array element per character
 print_r($array_of_str);

output : Array ( [0] => 101131110 [1] => 101131110 [2] => )
and then :

$i=0;
foreach($array_of_str as $array_of_chars){
//read the position of the preferred character
if($array_of_chars[$i]==$char){
echo "<br/> the position of 3 is in position number ".$i;
}
$i++;
}

output is empty , can you explain this syntax ?

Member Avatar for diafol

You're comparing the wrong data. What happened to the strpos?

$char = 3;
$pos = -1;
$out = "";
$r = array_filter(explode("|", $array));
foreach($r as $v)
{
    if($pos = strpos($v, $char, $pos+1)) !== false)
    {
        $out .= "Found at position $pos in $v\n";
    }else{
        $out .= "Not found in $v\n";
    }
}
echo nl2br($out);

Hi,

I'm not understanding what your goal is, so wondering if it can be achieved without looping.
Can you explain some more scenarios of the makeup of your strings (haystacks) and chars (needles)?

diafol and paulkd :

well, at first, i want to know number 3 position. may be, you can read again my first post,

$string = '101131110|101131110|';

and i get it, with strpos I find that position 3 at (5 and 15),

if character '|' ignored, then the position 3 at (5 and 14).
Now, I want to know how to ignore this character '|' when I use strpos so i get the position 3 at 5 and 14.
Or any other ideas ?

Now, I want to know how to ignore this character '|' when I use strpos so i get the position 3 at 5 and 14.

Remove the pipe from your string before finding the positions.

as per pritaeas suggestion...

$char   = '3';
$string = '101131110|101131110|';

$string = str_replace('|', '', $string);

$positions = array();
$pos = -1;
while (($pos = strpos($string, $char, $pos+1)) !== false) {
    $positions[] = $pos+1;
}

$result = implode(',', $positions);
print_r($result);

@paulkd : thank you paulkd, finished, problem has been resolved, thanks !!

How is the explode function different from the Position function ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.