PHP substr循环输出

PHP substr loop output

目前我的代码只输出第一行:fruit apple name stone

如何循环遍历 $string 文本以获取所有实例

如何让它也检查第二行并输出:fruit guava name roddy

例如在下面的 $string 文本中我们有:
foo name stoner 和 foo name roddy 目前我只能提取名称 stoner 和脚本停止。 我也希望它提取名称 roddy。

对不起,如果我没有道理。我的英文不太好

$string = 'foo name stoner loller bar php haystack needle fruit apples
foo name roddy koala bar php haystack needle fruit guavas';
$needle = 'fruit';
$needle1 = 'name';
$str = substr($string, strpos($string, $needle) + strlen($needle), 6);
$str1 = substr($string, strpos($string, $needle1) + strlen($needle1), 6);
echo  $needle. $str; 
echo " ";
echo  $needle1. $str1;

看看这个正则表达式,这是你想要的吗?

 $string = 'foo name stoner loller bar php haystack needle fruit apples
foo name roddy koala bar php haystack needle fruit guavas';
preg_match("/(name \w{6}).*(fruit \w{6})/", $string, $output);

输出:

array(3
  0 =>  name stoner loller bar php haystack needle fruit apples
  1 =>  name stoner
  2 =>  fruit apples
)
array(3
  0 =>  name roddy koala bar php haystack needle fruit guavas
  1 =>  name roddy
  2 =>  fruit guavas
 )

http://www.phpliveregex.com/p/fzF

编辑为仅匹配 6 个字符,但这意味着 "roddy" 行不匹配,因为它只有 5 个字符。

更新: 在您添加到您的问题的最后评论之后,我有点困惑,我想我没有正确理解您的问题,所以这个答案可能不会毕竟是你想要的...


我假设您想要的是按行拆分字符串并过滤那些包含该字符串的行。看看这个:

<?php

// No HTML output for now, so I added this to make it display properly in the browser
// You can remove this line if you are using `php -f` from the command line instead of a browser
header("Content-Type: text/plain");

// Added two more lines for clarification
$string = 'foo name stoner loller bar php haystack needle fruit apples
foo name roddy koala bar php haystack needle fruit guavas
something here which only contains fruit bla bla test
and this line has name but thats it bla bla bla';

// I guess you will have more needles later, so an array makes sense here
$needles = ['fruit', 'name', 'nothing'];

// Split the string into lines first
$lines = explode("\n", $string);

// Check each needle
foreach($needles as $needle) {
    // Output needle
    echo "Needle: $needle\n";

    // Filter the lines: Check if word is contained
    $matchingLines = array_filter($lines, function($line) use ($needle) {
        // Tells array_filter to include the line if the needle is contained in it
        return strpos($line, $needle) !== false;

        // Note that this currently searches the full line regardless of word
        // boundaries. If you wanted to find full words only (e.g. not `dragonfruit`
        // when searching for `fruit` or something), you could use this:
        //   return in_array($needle, explode(" ", $line));
    });

    // Output results, if any
    if(count($matchingLines)) {
        echo "Results:\n";
        echo implode("\n", $matchingLines);
    } else {
        echo "No results!";
    }
    echo "\n\n";
}

输出:

Needle: fruit
Results:
foo name stoner loller bar php haystack needle fruit apples
foo name roddy koala bar php haystack needle fruit guavas
something here which only contains fruit bla bla test

Needle: name
Results:
foo name stoner loller bar php haystack needle fruit apples
foo name roddy koala bar php haystack needle fruit guavas
and this line has name but thats it bla bla bla

Needle: nothing
No results!

因此,如果基于注释 foo 是一个行分隔符,并且该字符串本质上将被视为键 => 值的关联数组

<?php
    $string = 'foo name stoner loller bar php haystack needle fruit apples foo name roddy koala bar php haystack needle fruit guavas';

    //Get all of the arrays we will need to search
    $hayStacks = explode("foo", $string);

    foreach($hayStacks as $hayStack){
        $words = explode(' ', $hayStack);
        //Enter the key you want to find and then get the next element value.
        echo $words[array_search("name", $words)+1]."<br>";
        echo $words[array_search("fruit", $words)+1]."<br>";
    }
?>