/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] removing php code from a file

if i have a page that resembles:

[code=php]
<?php
echo “test1” ;
?>
<html>
<head></head>
<body>
<?php echo “test2” ; ?>
</body>
</html>
[/code]

how can i remove the php sections (from <?php to ?>) and replace with an html comment <!– code section 1 (number used to repopulate with code) –>

i will store the sections i remove in an array so i can reinsert them after i have done any work on the html code.

thanks guys

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisOct 22.2006 — Once you've read in the file content (and as long as the opening and closing PHP tags are adhered to), the something like this should work (untested):
[code=php]
$result = preg_match_all('/<?.*?>/s', $content, $matched_content);
$stripped_content = preg_replace('/<?.*?>/s', '', $content);
[/code]
Copy linkTweet thisAlerts:
@Sid3335authorOct 22.2006 — works when the php code is on a single line, but not if there is a linefeed.

my regular expression knowledge is non-existant
Copy linkTweet thisAlerts:
@bokehOct 22.2006 — The code above would fall over if any <?xml tags are present.

The following disassembles and reassembles the posted example. Would be better in object context. [code=php]<?php

header('Content-Type: text/plain');

$source_file = 'test.txt';

$destinationHTML = 'test2.txt';
$destinationPHP = 'test3.txt';

$reassembled = 'test4.txt';


# disassemble and save to two files
$contents = file_get_contents($source_file);
preg_match_all('/<?php.*??>/is', $contents, $matches);
$handle = fopen($destinationPHP, 'w') or die('I could not open: ' . $destinationPHP);
fwrite($handle, serialize($matches[0]));
fclose($handle);

$handle = fopen($destinationHTML, 'w') or die('I could not open: ' . $destinationHTML);
fwrite($handle, preg_replace_callback('/<?php.*??>/is', 'first_callback', $contents));
fclose($handle);

#reassemble and save to one file
$HTML = file_get_contents($destinationHTML);
$PHP = file_get_contents($destinationPHP);
$handle = fopen($reassembled, 'w') or die('I could not open: ' . $reassembled);
second_callback(null, true, $PHP);
fwrite($handle, preg_replace_callback('/<!-- code section (d+) -->/', 'second_callback', $HTML));

echo 'done';

function first_callback($in, $reset = false)
{
static $count = 0;
if($reset === true)
{
$count = 0;
return null;
}
return '<!-- code section '.($count++).' -->';
}

function second_callback($in, $reset = false, $PHP = null)
{
static $count = 0;
static $code;
if($reset === true)
{
$count = 0;
$code = unserialize($PHP);
return null;
}
if(isset($code[$in[1]]))
{
return $code[$in[1]];
}
return $in[0];
}

?>[/code]
Copy linkTweet thisAlerts:
@Sid3335authorOct 22.2006 — that is great, exactly what i was looking for.

thanks bokeh
Copy linkTweet thisAlerts:
@pcthugOct 22.2006 — Marked Resolved.
×

Success!

Help @Sid3335 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...