[SOLVED]editing array elements
Moderator: General Moderators
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
[SOLVED]editing array elements
i have an array where all the values are the names of text files. what is the best way to take out each value, remove the .txt extension and then place the resulting filename back into an array in the same order as the original array?
thanks in advance
thanks in advance
Last edited by rubberjohn on Tue May 24, 2005 2:20 pm, edited 2 times in total.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
off the top of my head, might not be the best way but would certainly work
Code: Select all
foreach ($array as $key => $value)
{
$array[$key] = str_replace('.txt', '', $value);
}And what if a file is named "something.txtfile.txt" ?
Use this instead:
Use this instead:
Code: Select all
foreach ($array as $key => $value) {
$array[$key] = preg_replace('/^(.+)\.txt$/i', '\\1', $value);
}-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
back to the original q for a sec
ok ive tried this
and this is what i get
what am i doing wrong? i dont think ive got the hang of 'foreach' yet
Code: Select all
foreach ($temp as $key => $value){
$temp[$key] = str_replace('.txt', '', $value);
print_r($value);
}Code: Select all
Array ( ї0] => greengame.txt ї1] => smalltable.txt ї2] => notes.txt ї3] => fishdrawn.txt ї4] => pattern.txt ї5] => girl.txt ї6] => net.txt ї7] => peddle.txt ї8] => table.txt ї9] => cross.txt ї10] => paving.txt ї11] => woodslice.txt ї12] => underwater.txt ї13] => yellowtap.txt ї14] => wood.txt ї15] => paint3.txt ї16] => coffee.txt ї17] => bath.txt ї18] => lacetable.txt ї19] => mosaic.txt ї20] => clock.txt )- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
print_r after you get out of the foreach

Code: Select all
foreach ($temp as $key => $value)
{
$temp[$key] = str_replace('.txt', '', $value);
}
print_r($temp);-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
same same
just tried that and the result is still the same and i tried the other way aswell
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
huh after some testing for some reason you need to just remove the . from .txt in the str replace fucntion
str_replace('txt', '', $value);
is what is should look like, i have no idea why the . messes it up, i even tried escaping it with a \.txt but that didnt work, theres your solution, just remove the . from the str replace but if anyone knows why this is the case then please tell me
str_replace('txt', '', $value);
is what is should look like, i have no idea why the . messes it up, i even tried escaping it with a \.txt but that didnt work, theres your solution, just remove the . from the str replace but if anyone knows why this is the case then please tell me
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
thanks
cheers shiznatix thats done the trick, i can finally get this finished now