PHP : load another php file source and replace all instances of string “”












0















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










share|improve this question




















  • 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











  • @ChrisForrence - thanks for the info and the link. I have edited the question

    – Louis Shraga
    Nov 20 '18 at 22:15
















0















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










share|improve this question




















  • 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











  • @ChrisForrence - thanks for the info and the link. I have edited the question

    – Louis Shraga
    Nov 20 '18 at 22:15














0












0








0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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











  • @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












3 Answers
3






active

oldest

votes


















2














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






share|improve this answer

































    2














    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)






    share|improve this answer































      0














      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);





      share|improve this answer





















      • 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











      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      2














      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






      share|improve this answer






























        2














        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






        share|improve this answer




























          2












          2








          2







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 22:37

























          answered Nov 20 '18 at 22:23









          PhilPhil

          98.1k11143161




          98.1k11143161

























              2














              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)






              share|improve this answer




























                2














                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)






                share|improve this answer


























                  2












                  2








                  2







                  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)






                  share|improve this answer













                  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)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 22:42









                  johannesjohannes

                  13k13355




                  13k13355























                      0














                      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);





                      share|improve this answer





















                      • 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
















                      0














                      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);





                      share|improve this answer





















                      • 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














                      0












                      0








                      0







                      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);





                      share|improve this answer















                      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);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 20 '18 at 22:26









                      Nick

                      33.2k132042




                      33.2k132042










                      answered Nov 20 '18 at 22:12









                      DharmanDharman

                      5,24462554




                      5,24462554








                      • 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





                        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


















                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?