我从我用preg_replace
清理过的文件中提取数据。
问题是它只返回$output
一次,即使文档中有多个$content
。
我需要使用循环但是我不知道如何让它工作。我尝试使用此link中的此代码,但无法使其在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;
?>
任何帮助都将不胜感激。
答案 0 :(得分:0)
使用此代码执行此操作:
<?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;
?>
答案 1 :(得分:0)
试试这个。你没有使用循环。我认为没有循环就无法检索多个值。
$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;
答案 2 :(得分:0)
我能够自己解决它:
<?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]);
}
?>