Jump to content

[SOLVED] Rearranging txt


newbtophp

Recommended Posts

I have a large txt which looks like this:

 

         7  blah...blahrandomtext                                       ~7      ~, '%7C'
         4  blah...blahrandomtext                                       ~7      ~7, 'html'
         5  blah...blahrandomtext                                       ~7      ~7, '+'
         6  blah...blahrandomtext                                       ~7      ~7, 'xmlns'
         7  blah...blahrandomtext                                       ~7      ~7, '%7D'
         8  blah...blahrandomtext                                       ~7      ~7, '%22'
         9  blah...blahrandomtext                                       ~7      ~7, 'http'
        10  blah...blahrandomtext                                       ~7      ~7, '%7A%2F%2F'
        11  blah...blahrandomtext                                       ~7      ~7, 'www'
        12  blah...blahrandomtext                                       ~7      ~7, '.'
        17  blah...blahrandomtext                                       ~7      ~7, 'w7'
        14  blah...blahrandomtext                                       ~7      ~7, '.'
        15  blah...blahrandomtext                                       ~7      ~7, 'org'
        16  blah...blahrandomtext                                       ~7      ~7, '%2F'
        17  blah...blahrandomtext                                       ~7      ~7, '1999'
        18  blah...blahrandomtext                                       ~7      ~7, '%2F'
        19  blah...blahrandomtext                                       ~7      ~7, 'xhtml'
        20  blah...blahrandomtext                                       ~7      ~7, '%22'

etc.

 

 

I want to rearrange it using php tolook like this:

 

 

%Chtml+xmlns%7D%22http%7A%2F%2Fwww etc.

 

To remove everything except the stuff within the single quotes and join them?

 

All help is greatly apreciated  :D

Link to comment
https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/
Share on other sites

Not sure, but you may want to look into ltrim()

That would be quite a trim..

you could get the string position of the first and last quote and use substring to pull it out,

 

but should work (Untested)

<?php
$lines = file('theFile.txt');
$data = "";
foreach ($lines as $line) {
    if (preg_match('/\'([^\']*)\'/', $line, $regs)) {
    	$data .= $regs[1];
    }
}
echo $data;

Very true, if this is a one off then your be okay but for less memory usage try

<?php
$handle = fopen("theFile.txt", "r");
if ($handle) {
    $data = "";
    while (!feof($handle)) {
        $line = fgets($handle, 4096);
        if (preg_match('/\'([^\']*)\'/', $line, $regs)) {
            $data .= $regs[1];
        }
    }
    fclose($handle);
    echo $data;
}

If the syntax is strictly as in your sample (i.e. with no single quotes in the random text), you could also use a single preg_match_all() call:

 

<?php
$source = file_get_contents('filename.txt');
preg_match_all('~\'([^\']+)\'~', $source, $matches);
$data = implode('', $matches[1]);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.