Php 不能多次循环输出
我正在从已用Php 不能多次循环输出,php,loops,Php,Loops,我正在从已用preg\u replace清理的文件中提取数据。 问题是它只返回一次$output,即使文档中有多个$content 我需要使用一个循环,但我不知道如何让它工作。我曾尝试使用此代码,但无法使其在echo$output方面正常工作 这是我的密码: <?php $getme = file_get_contents("somefile.txt"); $string = preg_replace('/[^A-Za-z\-]/', '', $getme); function ge
preg\u replace
清理的文件中提取数据。
问题是它只返回一次$output
,即使文档中有多个$content
我需要使用一个循环,但我不知道如何让它工作。我曾尝试使用此代码,但无法使其在echo$output
方面正常工作
这是我的密码:
<?php
$getme = file_get_contents("somefile.txt");
$string = preg_replace('/[^A-Za-z\-]/', '', $getme);
function get_between($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
$content = $string;
$start = "somestuff";
$end = "morestuff";
$output = get_between($content,$start,$end);
echo $output;
?>
任何帮助都将不胜感激。使用以下代码:
<?php
$getme = file_get_contents("somefile.txt");
$string = preg_replace('/[^A-Za-z\-]/', '', $getme);
function get_between($valuestr,$start,$end){
if (isset($valuestr)){
$r = explode($end, $valuestr);
return $r;
}
return '';
}
$content = $string;
$start = "somestuff";
$end = "morestuff";
//Explode all of input contents :
$r = explode($start, $content);
// Loop check function
foreach ($r as $valuestr) {
$output .= get_between($valuestr,$start,$end);
}
echo $output;
?>
试试这个。您没有使用循环。我认为在没有循环的情况下检索多个值是不可能的
$getme = file_get_contents("somefile.txt");
$string = preg_replace('/[^A-Za-z\-]/', '', $getme);
function get_between($content,$start,$end){
$retval='';
$r = explode($start, $content);
foreach($r as $s){
if (isset($s)){
$t = explode($end, $s);
$retval .=$t[0].'<br>';
}
}
return $retval;
}
$content = $string;
$start = "somestuff";
$end = "morestuff";
$output = get_between($content,$start,$end);
echo $output;
$getme=file\u get\u contents(“somefile.txt”);
$string=preg_replace('/[^A-Za-z\-]/',''$getme);
函数get_between($content,$start,$end){
$retval='';
$r=分解($start,$content);
foreach($r为$s){
如果(isset($s)){
$t=爆炸($end,$s);
$retval.=$t[0]。
;
}
}
返回$retval;
}
$content=$string;
$start=“something”;
$end=“morestuff”;
$output=get_-between($content,$start,$end);
echo$输出;
我能够自己解决这个问题:
<?php
$getme = file_get_contents("somefile.txt");
$string = preg_replace('/[^A-Za-z\-]/', '', $getme);
preg_match_all("/somestuff(.*?)morestuff/", $string, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
echo str_replace(array('somestuff', 'morestuff'), array('', ','), $val[0]);
}
?>
你到底想做什么?请添加somefile.txt的一些行,但我认为你必须在第一次爆炸后添加一个foreach循环。这根本不起作用。如果你在页面上添加一个somefile.txt
的示例内容,它可能会更好地帮助你确定你想要什么。Raju,这是可行的,但问题是它在文档中添加了大量的文本,而这些文本都是我从中提取的。这里有一个小例子ErrorFunctionTrierrtNewDateGetTimeeQjFashHandledDulploaderGzblFunctionEifWindowPerformanceWindowPerformanceTimeingWindowPerformanceEventsByTypevarnterHandleOtterLoader功能TntNonFn StartFunctionTv是所有BStStartDateNown非StartFunctionThisTimeDateNow此StartPositionPathNameLocation的安装程序