PHP : load another php file source and replace all instances of string “”
I have a .php file which is actually an SVG file with some inline PHP code. Lets call it inner.php
:
<?php
$uuid = uniqid();
?>
<svg class="__combo" id=<?php echo $uuid ?>
...
</svg>
I am writing another php file which should emit the content of inner.php but with all instances of <?php ... ?>
replaced by some string ( for example "AA").The file (lets call it outer.php) looks like this now :
<?php
$svg_body = file_get_contents("inner.php"));
$replaced = preg_replace("??","AA" , $svg_body);
echo "$replaced"
?>
I marked with "??" the part where I would like to put a regular expression to contain any string starting with "<?php"
and ending with first occurrence of "?>"
. And the output I expect to see is
"AA"
<svg class="__combo" id="AA"
...
</svg>
Basically, I dont find a way to escape string containing the <?php
and ?>
in PHP
php regex
add a comment |
I have a .php file which is actually an SVG file with some inline PHP code. Lets call it inner.php
:
<?php
$uuid = uniqid();
?>
<svg class="__combo" id=<?php echo $uuid ?>
...
</svg>
I am writing another php file which should emit the content of inner.php but with all instances of <?php ... ?>
replaced by some string ( for example "AA").The file (lets call it outer.php) looks like this now :
<?php
$svg_body = file_get_contents("inner.php"));
$replaced = preg_replace("??","AA" , $svg_body);
echo "$replaced"
?>
I marked with "??" the part where I would like to put a regular expression to contain any string starting with "<?php"
and ending with first occurrence of "?>"
. And the output I expect to see is
"AA"
<svg class="__combo" id="AA"
...
</svg>
Basically, I dont find a way to escape string containing the <?php
and ?>
in PHP
php regex
1
As a note,str_replace
replaces raw strings, but for regular expressions, you'll want to usepreg_replace
instead.
– Chris Forrence
Nov 20 '18 at 22:09
@ChrisForrence - thanks for the info and the link. I have edited the question
– Louis Shraga
Nov 20 '18 at 22:15
add a comment |
I have a .php file which is actually an SVG file with some inline PHP code. Lets call it inner.php
:
<?php
$uuid = uniqid();
?>
<svg class="__combo" id=<?php echo $uuid ?>
...
</svg>
I am writing another php file which should emit the content of inner.php but with all instances of <?php ... ?>
replaced by some string ( for example "AA").The file (lets call it outer.php) looks like this now :
<?php
$svg_body = file_get_contents("inner.php"));
$replaced = preg_replace("??","AA" , $svg_body);
echo "$replaced"
?>
I marked with "??" the part where I would like to put a regular expression to contain any string starting with "<?php"
and ending with first occurrence of "?>"
. And the output I expect to see is
"AA"
<svg class="__combo" id="AA"
...
</svg>
Basically, I dont find a way to escape string containing the <?php
and ?>
in PHP
php regex
I have a .php file which is actually an SVG file with some inline PHP code. Lets call it inner.php
:
<?php
$uuid = uniqid();
?>
<svg class="__combo" id=<?php echo $uuid ?>
...
</svg>
I am writing another php file which should emit the content of inner.php but with all instances of <?php ... ?>
replaced by some string ( for example "AA").The file (lets call it outer.php) looks like this now :
<?php
$svg_body = file_get_contents("inner.php"));
$replaced = preg_replace("??","AA" , $svg_body);
echo "$replaced"
?>
I marked with "??" the part where I would like to put a regular expression to contain any string starting with "<?php"
and ending with first occurrence of "?>"
. And the output I expect to see is
"AA"
<svg class="__combo" id="AA"
...
</svg>
Basically, I dont find a way to escape string containing the <?php
and ?>
in PHP
php regex
php regex
edited Nov 20 '18 at 22:14
Louis Shraga
asked Nov 20 '18 at 22:05
Louis ShragaLouis Shraga
1891215
1891215
1
As a note,str_replace
replaces raw strings, but for regular expressions, you'll want to usepreg_replace
instead.
– Chris Forrence
Nov 20 '18 at 22:09
@ChrisForrence - thanks for the info and the link. I have edited the question
– Louis Shraga
Nov 20 '18 at 22:15
add a comment |
1
As a note,str_replace
replaces raw strings, but for regular expressions, you'll want to usepreg_replace
instead.
– Chris Forrence
Nov 20 '18 at 22:09
@ChrisForrence - thanks for the info and the link. I have edited the question
– Louis Shraga
Nov 20 '18 at 22:15
1
1
As a note,
str_replace
replaces raw strings, but for regular expressions, you'll want to use preg_replace
instead.– Chris Forrence
Nov 20 '18 at 22:09
As a note,
str_replace
replaces raw strings, but for regular expressions, you'll want to use preg_replace
instead.– Chris Forrence
Nov 20 '18 at 22:09
@ChrisForrence - thanks for the info and the link. I have edited the question
– Louis Shraga
Nov 20 '18 at 22:15
@ChrisForrence - thanks for the info and the link. I have edited the question
– Louis Shraga
Nov 20 '18 at 22:15
add a comment |
3 Answers
3
active
oldest
votes
This expression seems to work fine...
preg_replace('/<?(phps|=).*?>/siU', '"AA"', $svg_body);
Demo ~ https://3v4l.org/0Ci9d
To break it down...
<?(phps|=)
- a literal match for"<?php"
(question-mark escaped) followed by a whitespace character (could be newline) or the short-echo<?=
.*
- zero or more characters
?>
- a literal match for"?>"
s
- sets*
to match over newlines, required for your first<?php ... ?>
block
i
- case-insensitive because why not
U
- ungreedy. Means*
matches stops at the first following pattern, not the last. This is to prevent*
from matching everything between the first"<?php"
and the last"?>"
See here for more information on the modifiers ~ http://php.net/manual/reference.pcre.pattern.modifiers.php
add a comment |
An alternative is using PHP's parser via the tokenizer Extension:
<?php
$tokens = token_get_all("<svg>n<?php echo; ?></svg>");
$result='';
$in_php = false;
foreach ($tokens as $token) {
if ($token[0]==T_INLINE_HTML) {
$result .= $token[1];
$in_php = false;
} else if (!$in_php) {
$result .= "AAA";
$in_php=true;
}
}
echo $result;
https://3v4l.org/jEpYi
This has the benefit, that it also handles other open tags, like <?=
and files without closing tag. And handles cases where the closing tag also appears in PHP code (I.e. in a comment)
add a comment |
Try doing this without RegEx first. You may try with substr_replace
.
$replaced = substr_replace($svg_body, 'AA', strpos($svg_body, '<?php'), strpos($svg_body, '?>'));
if you really need RegEx then try something like this:
$replaced = preg_replace(/(<?php[sS]+??>)/, "AA", $svg_body);
1
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402292%2fphp-load-another-php-file-source-and-replace-all-instances-of-string-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This expression seems to work fine...
preg_replace('/<?(phps|=).*?>/siU', '"AA"', $svg_body);
Demo ~ https://3v4l.org/0Ci9d
To break it down...
<?(phps|=)
- a literal match for"<?php"
(question-mark escaped) followed by a whitespace character (could be newline) or the short-echo<?=
.*
- zero or more characters
?>
- a literal match for"?>"
s
- sets*
to match over newlines, required for your first<?php ... ?>
block
i
- case-insensitive because why not
U
- ungreedy. Means*
matches stops at the first following pattern, not the last. This is to prevent*
from matching everything between the first"<?php"
and the last"?>"
See here for more information on the modifiers ~ http://php.net/manual/reference.pcre.pattern.modifiers.php
add a comment |
This expression seems to work fine...
preg_replace('/<?(phps|=).*?>/siU', '"AA"', $svg_body);
Demo ~ https://3v4l.org/0Ci9d
To break it down...
<?(phps|=)
- a literal match for"<?php"
(question-mark escaped) followed by a whitespace character (could be newline) or the short-echo<?=
.*
- zero or more characters
?>
- a literal match for"?>"
s
- sets*
to match over newlines, required for your first<?php ... ?>
block
i
- case-insensitive because why not
U
- ungreedy. Means*
matches stops at the first following pattern, not the last. This is to prevent*
from matching everything between the first"<?php"
and the last"?>"
See here for more information on the modifiers ~ http://php.net/manual/reference.pcre.pattern.modifiers.php
add a comment |
This expression seems to work fine...
preg_replace('/<?(phps|=).*?>/siU', '"AA"', $svg_body);
Demo ~ https://3v4l.org/0Ci9d
To break it down...
<?(phps|=)
- a literal match for"<?php"
(question-mark escaped) followed by a whitespace character (could be newline) or the short-echo<?=
.*
- zero or more characters
?>
- a literal match for"?>"
s
- sets*
to match over newlines, required for your first<?php ... ?>
block
i
- case-insensitive because why not
U
- ungreedy. Means*
matches stops at the first following pattern, not the last. This is to prevent*
from matching everything between the first"<?php"
and the last"?>"
See here for more information on the modifiers ~ http://php.net/manual/reference.pcre.pattern.modifiers.php
This expression seems to work fine...
preg_replace('/<?(phps|=).*?>/siU', '"AA"', $svg_body);
Demo ~ https://3v4l.org/0Ci9d
To break it down...
<?(phps|=)
- a literal match for"<?php"
(question-mark escaped) followed by a whitespace character (could be newline) or the short-echo<?=
.*
- zero or more characters
?>
- a literal match for"?>"
s
- sets*
to match over newlines, required for your first<?php ... ?>
block
i
- case-insensitive because why not
U
- ungreedy. Means*
matches stops at the first following pattern, not the last. This is to prevent*
from matching everything between the first"<?php"
and the last"?>"
See here for more information on the modifiers ~ http://php.net/manual/reference.pcre.pattern.modifiers.php
edited Nov 20 '18 at 22:37
answered Nov 20 '18 at 22:23
PhilPhil
98.1k11144161
98.1k11144161
add a comment |
add a comment |
An alternative is using PHP's parser via the tokenizer Extension:
<?php
$tokens = token_get_all("<svg>n<?php echo; ?></svg>");
$result='';
$in_php = false;
foreach ($tokens as $token) {
if ($token[0]==T_INLINE_HTML) {
$result .= $token[1];
$in_php = false;
} else if (!$in_php) {
$result .= "AAA";
$in_php=true;
}
}
echo $result;
https://3v4l.org/jEpYi
This has the benefit, that it also handles other open tags, like <?=
and files without closing tag. And handles cases where the closing tag also appears in PHP code (I.e. in a comment)
add a comment |
An alternative is using PHP's parser via the tokenizer Extension:
<?php
$tokens = token_get_all("<svg>n<?php echo; ?></svg>");
$result='';
$in_php = false;
foreach ($tokens as $token) {
if ($token[0]==T_INLINE_HTML) {
$result .= $token[1];
$in_php = false;
} else if (!$in_php) {
$result .= "AAA";
$in_php=true;
}
}
echo $result;
https://3v4l.org/jEpYi
This has the benefit, that it also handles other open tags, like <?=
and files without closing tag. And handles cases where the closing tag also appears in PHP code (I.e. in a comment)
add a comment |
An alternative is using PHP's parser via the tokenizer Extension:
<?php
$tokens = token_get_all("<svg>n<?php echo; ?></svg>");
$result='';
$in_php = false;
foreach ($tokens as $token) {
if ($token[0]==T_INLINE_HTML) {
$result .= $token[1];
$in_php = false;
} else if (!$in_php) {
$result .= "AAA";
$in_php=true;
}
}
echo $result;
https://3v4l.org/jEpYi
This has the benefit, that it also handles other open tags, like <?=
and files without closing tag. And handles cases where the closing tag also appears in PHP code (I.e. in a comment)
An alternative is using PHP's parser via the tokenizer Extension:
<?php
$tokens = token_get_all("<svg>n<?php echo; ?></svg>");
$result='';
$in_php = false;
foreach ($tokens as $token) {
if ($token[0]==T_INLINE_HTML) {
$result .= $token[1];
$in_php = false;
} else if (!$in_php) {
$result .= "AAA";
$in_php=true;
}
}
echo $result;
https://3v4l.org/jEpYi
This has the benefit, that it also handles other open tags, like <?=
and files without closing tag. And handles cases where the closing tag also appears in PHP code (I.e. in a comment)
answered Nov 20 '18 at 22:42
johannesjohannes
13k13355
13k13355
add a comment |
add a comment |
Try doing this without RegEx first. You may try with substr_replace
.
$replaced = substr_replace($svg_body, 'AA', strpos($svg_body, '<?php'), strpos($svg_body, '?>'));
if you really need RegEx then try something like this:
$replaced = preg_replace(/(<?php[sS]+??>)/, "AA", $svg_body);
1
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
add a comment |
Try doing this without RegEx first. You may try with substr_replace
.
$replaced = substr_replace($svg_body, 'AA', strpos($svg_body, '<?php'), strpos($svg_body, '?>'));
if you really need RegEx then try something like this:
$replaced = preg_replace(/(<?php[sS]+??>)/, "AA", $svg_body);
1
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
add a comment |
Try doing this without RegEx first. You may try with substr_replace
.
$replaced = substr_replace($svg_body, 'AA', strpos($svg_body, '<?php'), strpos($svg_body, '?>'));
if you really need RegEx then try something like this:
$replaced = preg_replace(/(<?php[sS]+??>)/, "AA", $svg_body);
Try doing this without RegEx first. You may try with substr_replace
.
$replaced = substr_replace($svg_body, 'AA', strpos($svg_body, '<?php'), strpos($svg_body, '?>'));
if you really need RegEx then try something like this:
$replaced = preg_replace(/(<?php[sS]+??>)/, "AA", $svg_body);
edited Nov 20 '18 at 22:26
Nick
33.3k132042
33.3k132042
answered Nov 20 '18 at 22:12
DharmanDharman
5,26562554
5,26562554
1
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
add a comment |
1
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
1
1
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Won't this only replace the first occurrence?
– Phil
Nov 20 '18 at 22:17
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Then loop it! OP didn't specify how many times the needle would appear in the haystack.
– Dharman
Nov 20 '18 at 22:25
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
Well, it's at least twice in their example
– Phil
Nov 20 '18 at 22:27
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
@Dharman - thanks, but I did say "all instances". Anyway, your answer is still educational
– Louis Shraga
Nov 21 '18 at 7:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402292%2fphp-load-another-php-file-source-and-replace-all-instances-of-string-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
As a note,
str_replace
replaces raw strings, but for regular expressions, you'll want to usepreg_replace
instead.– Chris Forrence
Nov 20 '18 at 22:09
@ChrisForrence - thanks for the info and the link. I have edited the question
– Louis Shraga
Nov 20 '18 at 22:15