0
$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

Edited by ivanichi

6
Contributors
9
Replies
48
Views
4 Years
Discussion Span
Last Post by annaharris
0

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++;
}

Edited by masterjiraya

0

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 ?

Edited by ivanichi

0

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);
0

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)?

0

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 ?

Edited by ivanichi

0

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.

0

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);
0

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

Edited by ivanichi

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.