1

I am pulling data from a file that I have cleaned up with preg_replace. Problem is it's only returning the $output once even though there is multiple $content through out the document.

I need to use a loop however I have no clue how to get it to work. I have tried to use this code from this link but can't get it to work properly in terms of echo $output.

Here is my code :

<?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;
?>

Any help would be appreciated.

Community
  • 1
  • 1
Joey Troy
  • 45
  • 6

3 Answers3

0

Do it with this code :

<?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;
?>
Root
  • 2,269
  • 5
  • 29
  • 58
0

Try this. You did not use loop . I think not possible to retrieve multiple value without loop.

$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;
raju gupta
  • 59
  • 1
  • 5
  • Raju, this works but the problem is it added a ton of text into the document from the document I was pulling from. Here is a small example `errorfunctiontrierrtnewDategetTimeeeQJfaxhandleDDuLPloaderGzBlfunctiontfunctioneifwindowperformancewindowperformancetimingwindowperformancegetEntriesByTypevarnteerthandleottloaderfeaturesstntnonfn-startfunctiontvareteinstanceofEventthisbstStartDatenownonfn-startfunctionthistimeDatenowthisstartPathlocationpathnamelocationhas` – Joey Troy Apr 16 '15 at 05:44
0

I was able to solve it on my own:

<?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]);
}

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Joey Troy
  • 45
  • 6