Search code examples
phpregexpreg-replacetext-parsingredaction

Remove or obscure text scraped from a web address


Following up from my last question @ stackoverflow.com/questions/7049245/

I got a couple of answers with the perfect regex code I was looking for. But now I have a new problem, I can't seem to get any regex code with a PHP preg_replace() working. I have been searching but no success.

The current PHP code I have is:

$nclist = file_get_contents('**REMOVED LINK**');
$thenc = explode("\n",$nclist);
foreach ($thenc as $row)
{
    $nc .= $row."<br> ";
}

$search = array (
    '^(.*)(\((?:-?\d{1,4}\.\d{1}(?:, |\))){3} to \((?:-?\d{1,4}\.\d{1}(?:, |\))){3})(?= distance)(.*)$/ && do { my ($pre, $no_numbers, $post) = ($1, $2, $3); $no_numbers =~ s/\d+\.\d+/#/g; print "$pre$no_numbers$post\n"; }'
);

$replace = array ('');
$final = $nc;     
echo preg_replace($search, $replace, $final);
print_r($cheat);

And it displays the output of $nc fine, but doesn't want to apply the regex against it.

Also if you didn't see the last question I had, I needed all the parts that matched

(-90.8, 64.0, 167.5) to (-90.7, 64.0, 167.3)

removed, or at least censored into

(#, #, #) to (#, #, #)

Again, the regex answers from the last question worked perfectly, so I would like to use that.

EDIT1: Ah, I remembered I had the print_r() there to test something else, so I removed it, but now its just a blank page.


Solution

  • For each row:

    $row = preg_replace( '/(?:\-|\b)\d{1,4}.\d{1}\b(?=.*distance)/', '#', $row );
    

    Complete code:

    $contents = file_get_contents('http://dreamphreak.com/pwn9/yasmp/nocheat.php');
    $rows = explode( "\n", $contents );
    $new_contents = '';
    foreach ( $rows as $row ) {
        $row = preg_replace( '/(?:\-|\b)\d{1,4}.\d{1}\b(?=.*distance)/', '#', $row );
        $new_contents .= $row."<br> ";
    }
    
    echo $new_contents;