I'm not sure I fully understand what you are trying to do.
This line $text = file_get_contents(basename($_SERVER['PHP_SELF']));
resolves to a local filename which will be the name of the script it's included in.
It won't load the page which that script would output, it will load the file without executing the code. The contents of $text
will be a string containing the pure php content.
When I run your code from the command line and use "Here is a test of {your code}" as my string the output is:
{your code}<?php
require_once 'includetest.php';
print_r(basename($_SERVER['PHP_SELF']));
echo "Here's a test of Test";
If I run it from a browser and view the source I see that too, but the browser escapes the php so it isn't rendered on the page. So (for me at least), your concept kinda works. However, the fact the code isn't executed means it will never do what you intend.
Here's what I don't really understand though. In essence within index.php what you are telling your code to do is load a script which will load another copy of index.php and replace content within it.
If you want to change the behaviour of index.php then alter the code within index.php, don't generate unsuitable output and then load another script in an attempt to parse it before returning it. Just output it the way you want the first time.
Where is the content within the {}
that you want to remove coming from? Why do you want/need to remove it? If you can clarify exactly what your aim is then it will be easier to suggest a solution.