PHP子字符串循环输出


PHP substr loop output

如何循环$string文本以获得当前所有实例我的代码只输出第一行:fruit-apple-name-stone

我如何使它也检查第二行和输出:水果番石榴名称roddy

例如,在下面的$string文本中,我们有:
foo-namestoner和foo-nameroddy目前我只能提取斯通的名字和脚本停止。我也希望它提取名称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>";
    }
?>