You are here: Re: Delete last 15 chars from a file « PHP « IT news, forums, messages
Re: Delete last 15 chars from a file

Posted by M. Sokolewicz on 03/02/05 14:10

Shaun wrote:

> Hi,
>
> I am trying to create an XML file, it will be done in stages so if the file
> isn't present I will add the line:
>
> <?xml version="1.0" ?>
> <root_element>
>
> Then for each line I add I add the following
>
> <child_element>data<child_element>
>
> But for various reasons the application won't know when the file is complete
> so it won't have the final:
>
> </root_element>
>
> To solve this I intend to add the following each time
>
> <child_element>data<child_element>
> </root_element>
>
> and delete the </root_element> every time I add a new
> <child_element>data<child_element>. Can anyone tell me how I can delete the
> last 15 characters from a file every time I open the file?
>
> Thanks for your help
first of all, why don't you simply do something like:
<?php
$contents = file_get_contents('file');
$contents = str_replace('</root_element>', '', $contents);
$contents .= "<child_element>data<child_element></root_element>";
file_put_contents('file', $contents);
?>

But well, to strip off the last 15 chars, simply use substr(), thus:
$contents = substr(file_get_contents('file'), 0, -15);

that will return the entire file contents - the last 15 chars.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация