Hi all, I need help with one script.
If i Have string like

$str = "0001564[B]A[/B]58749655

I need to find that A in this string, and count which number is it (When tha A is on the third place, return 2 [starting from 0])
A think regular expression will be the best fo it, but i don't know how to put it together.
Please help me!
Thank your really much.

Recommended Answers

All 2 Replies

I think your problem needs to be split into two parts. First, finding the letter(s). Second, finding the position.

$string = '0001564AC587B4965B5';
$positions = array();

// Find all letters
preg_match_all('/[a-z]/i', $string, $matches);

// Check if any letters found
if(isset($matches[0])) {
    $offset = 0;

    // Iterate through each letter and find corresponding position
    foreach($matches[0] as $letter) {
        $position = strpos($string, $letter, $offset);

        if($position <> false) {
            $positions[] = "{$letter}: {$position}";
            $offset = $position + 1;

echo '<pre>'; print_r($positions); echo '</pre>'; die;

Yeah, that what I was looking for .. thanks man!

Be a part of the DaniWeb community

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