views:

23

answers:

4

Hi there. First time i post here and hope somebody will be able to help me.

I have a file whos numbering starts at 610 and goes on to 1019. I want to use PHP's preg_match() function to start the numbering from 0 and go on till 410.

Here is some code i've been working on. But i cant get the function to replace the numbers. I don't know why and i don't get any errors.

<?php

$string = "610 611 612 613 614 615 616 617"; //this isnt the actual file but will do. The actual file is more complicated. This is just a test string.

$patterns = array();
for ($i=610; $i<1020; $i++) {
    $patterns[$i] = '/$i/';
}

$replacements = array();
for ($j=1; $j<410; $j++) {
    $replacements[$j] = '\r\n' . $j;
}

$newText = preg_replace($patterns, $replacements, $string);
echo $newText;

?>

I used Example #2 form http://www.php.net/manual/en/function.preg-replace.php as reference.

Thanks in advance for any help :)

A: 

This won't do?

implode(" ", range(0, 410))

It seems odd that you want to change them "in place".

Artefacto
beaten to it :) exactly what I was thinking.
Darragh
You missed this comment: `//this isnt the actual file but will do. The actual file is more complicated. This is just a test string.`
NullUserException
@Null No, I read it. But it doesn't provide any insight. If anything, it could make the regex solution less desirable because if text followed, it could contain some of the numbers.
Artefacto
A: 

Your "patterns" array looks like this:

$patterns (
   610 => '/$i/',
   611 => '/$i/',
...
}

You need to use double quotes on line 7:

$patterns[$i] = "/$i/";
Piskvor
A: 

Don't bother with regular expressions for such a simple case... Simply use str_replace. It'll be faster, and equivalent to your present code...

$patterns = array();
for ($i=610; $i<1020; $i++) {
    $patterns[] = $i;
}

$replacements = array();
for ($j=1; $j<410; $j++) {
    $replacements[] = '\r\n' . $j;
}

$string = str_replace($patterns, $replacements, $string);

Now, you'd still need to use preg_replace if the patterns are more complicated (such as only searching for the start of the line, etc)... But for such a simple pattern, it's not worth it (IMHO)...

ircmaxell
`'\r\n'` won't be a newline. `str_replace()` is also not really appropriate for this (think word boundaries)
NullUserException
I know that. But he said he was dealing with files. So I assumed that he knew it won't be a new line and wanted to put a literal `\r\n` in there... I wasn't sure if that was an error or explicit, so I just went with what (s)he provided...
ircmaxell
@irc Yeah, it would help if the OP provided actual input and expected output files
NullUserException
A: 

Whoa! Didnt get an email till now that there was replies here. Sorry i havnt posted! Thanks so much for your help guys.

@ircmaxell and others who want to know more, here's a snippet of the file i want to change:

610 //this is the numbers i'd like to replace
01:17:52,672 --> 01:17:57,588 //nothing changes here
So as far as I know, they found Magdas body
after six hours.

611 //and this one needs changing
01:17:57,792 --> 01:18:00,909
There was a farmer who saw the smoke from the fire.

612
01:18:01,112 --> 01:18:04,343
He thought it was a party.

613
01:18:05,872 --> 01:18:09,262
Someone has cut the fat from the body.

614
01:18:11,232 --> 01:18:16,625
Yes, that is true. they found residues
of fat along with the head.

615
01:18:17,952 --> 01:18:20,023
Discusting.

So i cant just do a simple str_replace() because it will also replace some of the timers.

Now i want to take the numbers 610 through 1020 and replace them with numbers starting from 1 through to 410. With the code i wrote i dont get any output. Nothing happens i dont know whats wrong.

I'm using regular expressions because i want to differentiate between the headings and the timers and the only way i seem to do it is to find the line break before the heading. And finding a sequence seems easy with regex.

I want it to look like this

1 
01:17:52,672 --> 01:17:57,588
So as far as I know, they found Magdas body
after six hours.

2
01:17:57,792 --> 01:18:00,909
There was a farmer who saw the smoke from the fire.

3
01:18:01,112 --> 01:18:04,343
He thought it was a party.

4
01:18:05,872 --> 01:18:09,262
Someone has cut the fat from the body.

5
01:18:11,232 --> 01:18:16,625
Yes, that is true. they found residues
of fat along with the head.

6
01:18:17,952 --> 01:18:20,023
Discusting.

At all of you who have posted sollutions thank you. I cant test it right now as i have to go somewhere. I'll try them a bit later today. Thanks! I will post results asap!

I am open to all suggestions as to how to aproach this in a different way :)

Thanks again.

And i hope i cleared some things up :)

PhPNovice
@PhPNovice - This is not an answer. Please edit your question to reflect this information and delete this.
Peter Ajtai