I have this output(from another system) that I need the Test field to be on one line.
This stupid system wordwraps at 45 characters (with like 30 blank spaces before each line)
Here is my example output (that I need to input)
Name:
Pepsi
Test:
The Result was blah
and blah
Tester:
John
Name:
Sprite
Test:
The result was negative
Tester:
Jane
Etc etc
Sometimes the line after Test: gets word wrapped (some times not)
I need that line to be un-wordwrapped so I can import it in access.
The file is about 2mb, and there are a lot of instances that need to be cleaned up. That is why I am trying to write this script.
Thanks
—————-EDIT————-
This is what I have come up with so far. But I cant get it to replace
<?php
function replace_newline($string) {
return (string)str_replace(array("\r", "\r\n", "\n", " ", " ", " ", " "), ' ', $string);
}
function GetBetween($content,$start,$end){
$r = explode($start, $content);
foreach($r as $value){
$t = explode($end, $value); //$t[0] between value
$result = trim(preg_replace('/[\t\r\n]+/', ' ', trim($t[0])));
$result = trim($result);
$result = replace_newline($result);
if ( !strstr($result, "Name:") ) {
echo $result . "\r\n";
$test = str_replace($t[0], $result, $test);
}
}
}
$test= file_get_contents("4321.txt");
GetBetween($test, "Test:", "Tester:");
?>
This outputs:
The Result was blah and blah
The result was negative
This probably isn’t working code, but you get the idea:
You could probably do this whole thing with one crazy block of regex, but I’m not in the mood to work it out.
I know I said I wouldn’t use regex, but here goes:
I haven’t tested the code, but the comments should be a bit of guidance.
Note that this only works if 1) only labels contain a semicolon and 2) labels are only one word long. Also, you won’t want to run this on massive data sets. It’s not optimized for that sort of things. It’s optimized for quick-n-dirty.