开发者

Regex + Php preg_replace

开发者 https://www.devze.com 2023-03-28 01:56 出处:网络
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 cant seem to get any

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 cant seem to get any reg开发者_运维问答ex code with a php preg_replace working. I have been searching but no success.

The current code i have is:

(PHP)
$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 doesnt want to apply the regex against it. Any php help on how to get it working please? Thanks

Also if you didnt 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.


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;


Wouldn't this be easier?

$contents = file_get_contents('http://dreamphreak.com/pwn9/yasmp/nocheat.php');
$contents = preg_replace( '/\([0-9\.,\- ]+\) to \([0-9\.,\- ]+\)/', '', $contents );

echo $contents;

Working on my web server.

The reason you were having trouble with the page not loading when using the code that 'Nobody' gave you is that you're hitting the maximum page execution time for your server. It worked with a small sample of data because it can do that in under 30 seconds (or whatever your server's max execution time is. Mine is 30), but the huge piece of live data is taking it too long to process.

I tried doing a speed comparison between the two but I also couldn't get the other solution to work due to the timeout. Instead I took a sample of 40 lines from your live data and ran a speed test on both solutions using that.

My solution averaged 0.0001 seconds in 10 runs Nobody's solution averaged 0.0007 seconds in 10 runs

I'd suggest finding a way to do the whole thing without having to individually loop over each line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号