4 Replies Latest reply: Jun 9, 2014 11:40 AM by bregent RSS

    Searching/Replacing Large Chunks of Code via PHP

    jyeager11 Community Member

      My knowledge of PHP is as basic as it gets, and is generally limited to using variables and file includes. Every once in a while, I'll be able to adapt an existing operation to my purposes but right now, I'm struggling with one that I desperately need to get working; as it would solve so many of my current obstacles. It searches and replaces text in real-time, before the page is served to the end user.

      //creating markers

          $titleBegin = "<title>";

          $titleEnd = "</title>";

       

      //parsing

          $parsedTitle1 = explode($titleBegin, $content);

          $parsedTitle2 = explode($titleEnd, $parsedTitle1[1]);

          $content = $parsedTitle1[0] . $titleBegin . "This is the new title" . $titleEnd . $parsedTitle2[1];

       

          echo $content;

       

      This code was created to rewrite <title>Anything</title> into <title>This is the new title</title> in real-time, as the page is being served to the end user. The previous (unchanged) title remains hard-coded in the file, but the user sees the new one. And if I understand correctly, it does this by first dividing the targeted code in 3 parts : the opening marker, the text being modified, the closing marker. Then, it tells the code to modify the middle part.

       

      So far so good, right? I chose <title></title> in this example because those tags are typically unique in a web page. However, I don't know how to target (for example) only the 3rd pair of <h1></h1> tags in a given page. Because this would involve using multiple lines of code in each marker, and I'm not sure how to treat those line breaks. For instance, if I want to make this the first marker...

      <script type="text/javascript">

        function pmc_ads_interruptus_single() {

       

      ...how do I reference it in PHP? I've tried escaping the quote characters and ignoring the line change like it's not there...

      $markerBegin = "<script type=\"text/javascript\">function pmc_ads_interruptus_single() {"

       

      ...as well as treating it like a space...

      $markerBegin = "<script type=\"text/javascript\"> function pmc_ads_interruptus_single() {"

       

      ...but I can't get it working.

       

      Are there more characters than just the quote marks that need to be escaped? What might I be doing wrong?

        • 1. Re: Searching/Replacing Large Chunks of Code via PHP
          Nancy O. CommunityMVP

          I guess I'm missing something important because each page needs a unique page title for SEO reasons.  If you globally replace existing titles with a generic one, you're shooting yourself in the foot with Google and other SEs.

           

          Maybe what you're trying to do is echo contents of your title variable into a page heading, something like this?

           

          <title><?php $title="NEW PAGE TITLE"; ?><?php echo $title ; ?></title>

          </head>

           

          <body>

          <h3><?php echo $title ; ?></h3>

           

           

           

          Nancy O.

          • 2. Re: Searching/Replacing Large Chunks of Code via PHP
            bregent CommunityMVP

            It might help if you explain why do you need static content if it's going to be replaced with dynamic content? Why not just always have it dynamic? It would also help to see the entire code you are working with.

            • 3. Re: Searching/Replacing Large Chunks of Code via PHP
              jyeager11 Community Member

              Your heart's in the right place, but I'm really just trying to learn how to replace (or remove) large chunks of text on the fly via PHP. I only used it because it was an easy example of a short tag that's usually unique in a web page source code.

               

              Let's say I want to remove an entire portion of text non-destructively, as it's being served to the end user. For instance, a specific javascript. There's a 50-line javascript in my web page that I want removed or replaced non-destructively. Rather than str_replace() the entire code with "", I was told that using explode() to divide the sections I want to affect into 3 (starting marker, middle, end marker) so that if something changed in the middle area later on, the entire section will still get recognized by the PHP commands looking to re-write it on the output.

               

              Normally, a javascript would begin with <script type="text/javascript"> and end with </script>. But I can't target that lone (like I did <title></title>) because there will be several instances of both <script type="text/javascript"> and </title> in the page and I only want to affect one of them.

               

              This means I'll have to quote more code in my markers to make sure they're unique.

               

              In other words, how do I do this...

              $markerBegin = "
              <script type=\"text/javascript\">
                function pmc_ads_interruptus_single() {
              "

              ...and have it working? Right now, it's not recognizing those 2 lines in the code. But they're there. There's something wrong with my syntax.

               

              I did think to escape the quotation marks with backslashes, but is there anything else I need to re-interpret for the server to recognize what I'm targeting? For instance, is there an "escape" code for line breaks too? Do I need to add something between \"> and function for those two lines to be recognized when PHP instructs the page to look for them?

              • 4. Re: Searching/Replacing Large Chunks of Code via PHP
                bregent CommunityMVP

                Have you tried using Heredoc syntax?

                 

                PHP: Strings - Manual