如何遍历$ string文本以获取所有实例,此时我的代码只输出第一行:fruit apple name stone
我如何制作它还检查第二行并输出:fruit guava name roddy
例如在下面的$ string文本中我们有:
foo name stoner和foo name roddy
目前我只能提取名称stoner和脚本停止。
我也希望它能提取名字。
$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;
答案 0 :(得分:0)
看看这个正则表达式,这是你想要的吗?
$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个字符。
答案 1 :(得分:0)
更新:在您添加到问题的最后评论之后,我有点困惑,我认为我没有正确理解您的问题,所以这个答案可能不完全是您想要的毕竟......
我认为你想要的是用线分割字符串并过滤那些包含字符串的行。看看这个:
<?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!
答案 2 :(得分:0)
因此,如果基于注释foo
是行分隔符,则该字符串基本上被视为key =&gt;的关联数组;价值
<?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>";
}
?>