3

Lets say I have this data in a file (externalfile.txt)

#1#First#/1#
#2#Something#/2#

#end#

I want to write text between #/2# and #end#, How to do this?

2
  • 1
    I realise this might be due to a language barrier, but the words please and thank you might get you further. Commented Aug 26, 2012 at 9:10
  • Please can you tell me how to accomplish this? Commented Aug 26, 2012 at 9:20

2 Answers 2

3

Not sure if it's the proper way but you may take a look

$newval='new text';
$file_contents = file_get_contents('externalfile.txt');
file_put_contents('externalfile.txt', preg_replace("/#\/2#/", "#/2#\n$newval\n", $file_contents));

It works, anyway.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was a bit confused :-)
1

This PHP code inserts a new text right before the last occurence of the #end# string:

$str = file_get_contents('file.txt');
$splitted = explode('#end#',$str);
$pos = count($splitted)-2;
$splitted[$pos] .= 'new text';
$str = implode('#end#',$splitted);
echo $str;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.