PHP Find all occurrences of a substring in a string

后端 未结 9 1825
执念已碎
执念已碎 2020-12-01 01:56

I need to parse an HTML document and to find all occurrences of string asdf in it.

I currently have the HTML loaded into a string variable. I would just

相关标签:
9条回答
  • 2020-12-01 02:29
    <?php
    $mainString = "ffffdjmnpfffffdjmnpffff";
    $needle = "jmnp";
    $lastPos = 0;
    $positions = array();
    
    while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
        $positions[] = $lastPos;
        $lastPos = $lastPos + strlen($needle);
    }
    
    // Displays 3 and 10
    foreach ($positions as $value) {
        echo $value ."<br />";
    }
    ?>
    
    0 讨论(0)
  • 2020-12-01 02:31

    Simple strpos_all() function.

    function strpos_all($haystack, $needle_regex)
    {
        preg_match_all('/' . $needle_regex . '/', $haystack, $matches, PREG_OFFSET_CAPTURE);
        return array_map(function ($v) {
            return $v[1];
        }, $matches[0]);
    }
    

    Usage: Simple string as needle.

    $html = "ffffdasdfffffdasdffff";
    $needle = "asdf";
    
    $all_positions = strpos_all($html, $needle);
    var_dump($all_positions);
    

    Output:

    array(2) {
      [0]=>
      int(3)
      [1]=>
      int(10)
    }
    

    Or with regex as needle.

    $html = "ffffdasdfffffdasdffff";
    $needle = "[d]{3}";
    
    $all_positions = strpos_all($html, $needle);
    var_dump($all_positions);
    

    Output:

    array(2) {
      [0]=>
      int(0)
      [1]=>
      int(7)
    }
    
    0 讨论(0)
  • 2020-12-01 02:37

    Without using regex, something like this should work for returning the string positions:

    $html = "ffffdasdfffffdasdffff";
    $needle = "asdf";
    $lastPos = 0;
    $positions = array();
    
    while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
        $positions[] = $lastPos;
        $lastPos = $lastPos + strlen($needle);
    }
    
    // Displays 3 and 10
    foreach ($positions as $value) {
        echo $value ."<br />";
    }
    
    0 讨论(0)
提交回复
热议问题