3

In PHP, if I have a long string, IE 10'000 chars, how would you suggest I go about finding the first occurence of a certain string before and after a given position.

IE, if I have the string:

BaaaaBcccccHELLOcccccBaaaaB

I can use strpos to find the position of HELLO. How could I then go about finding the position of the first occurence of B before HELLO and the first occurence of B after HELLO?

Juicy
  • 11,840
  • 35
  • 123
  • 212

3 Answers3

1

You can use stripos() and strripos() to find the first occurrence of a sub-string inside a string. You can also supply a negative offset to strripos() function to search in reverse order (from right to left). strripos() with negative offset

$body = "BaaaaBcccccHELLOcccccBaaaaB";
$indexOfHello = stripos($body, 'Hello');
if ($indexOfHello !== FALSE)
{
    // First Occurrence of B before Hello
    $indexOfB= stripos(substr($body,0,$indexOfHello),'B',($indexOfHello * -1));
    print("First Occurance of B before Hello is ".$indexOfB."\n") ;
    
    // First Occurrence of B before Hello (in reverse order)
    $indexOfB= strripos($body,'B',($indexOfHello * -1));
    print("First Occurrence of B before Hello (in reverse order) is ".$indexOfB."\n") ;
    
    // First Occurrence of B after Hello
    $indexOfB= stripos($body,'B',$indexOfHello);
    print("First Occurance of B after Hello is ".$indexOfB."\n") ;
}
Usman Shaukat
  • 1,315
  • 16
  • 22
0

If you think about optimization there a lot of pattern search algorithms

Here is sample of naive pattern search:

/**
 * Naive algorithm for Pattern Searching
*/
function search(string $pat, string $txt, int $searchFrom = 0, ?int $searchTill = null) 
{ 
    $M = strlen($pat); 
    $N = strlen($txt); 

    if ($searchTill !== null && $searchTill < $N){
        $N = $searchTill;
    }

    for ($i = $searchFrom; $i <= $N - $M; $i++) 
    { 
  
        // For current index i,  
        // check for pattern match  
        for ($j = 0; $j < $M; $j++) 
            if ($txt[$i + $j] != $pat[$j]) 
                break; 
  
        // if pat[0...M-1] =  
        // txt[i, i+1, ...i+M-1] 
        if ($j == $M)  
            return $i;
    } 
} 

// Driver Code 
$txt = "BaaaaBcccccHELLOcccccBaaaaB"; 
if (null!==($helloPos = search("HELLO", $txt))){

    print("First Occurance of B before Hello is ".search("B", $txt, 0, $helloPos)."<br>") ;
    print("First Occurance of B after Hello is ".search("B", $txt, $helloPos, null)."<br>") ;
}
Urmat Zhenaliev
  • 1,497
  • 8
  • 22
-1

Given the position…

To find the first occurrence before, you can take the substr() before the match and use strrpos().

To find the first occurrence after, you can still use strpos() and set the offset parameter.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • But for the first occurrence before, if I take the substr before the match (ie: substr between 0 and POSITION_OF_HELLO) then how can I guarantee he will return the last match in that substr (the last match in that substr will be the match just before HELLO). If we look at the string I gave in example, the substr would be BaaaaBccccc, but strpos will return the first B. – Juicy Sep 11 '13 at 00:55
  • This is not a solution. Everyone knows such functions. – Federico Schiocchet May 20 '21 at 11:33