0

I am trying to strip all the PHP tag from the PHP file. I am trying to remove PHP tags because I want to get all the html href value's of all a tags. I don't know whether is it possible or not? Can anyone help me to resolve this issue? Thanks in advance.

$removedPhptags = preg_replace('/^<\?php(.*)(\?>)?$/s', '$1', "http://localhost/project/test.php");

test.php

        <html>
        <head>
        <title>Test</title>
        </head>
        <body>
        <div> <a href="test1.html" title="test"></a>
          <?php

         include './menulink.php'; 

        $result_array="123";

        if ($result_array=="123")
        {
        ?>
          <a href="test4.html" title="tesdfhdfhdfht2"></a>
          <?php 


        else{

        ?>
          <a href="test5.html" title="tedfhdfhst"></a>
          <?php


        }

        ?>
        </div>
        </body>
        </html>

menulink.php

                <html>
            <head>
            <title>Test</title>
            </head>
            <body>
            <div> <a href="menulink1.html" title="test">m1</a> <a href="menulink2.html" title="test2">m2</a>
              <?php

            $result_array="123";

               if ($result_array=="123")
            {
            ?>
              <a href="menulink3.html" title="tedfhdfhst">m3</a> <a href="menulink4.html" title="tesdfhdfhdfht2">m4</a>
              <?php 
            }

            else{

            ?>
              <a href="menulink5.html" title="tedfhdfhst">m5</a> <a href="menulink6.html" title="tesdfhdfhdfht2">m6</a>
              <?php


            }

            ?>
            </div>
            </body>
            </html>
saranchel n
  • 533
  • 1
  • 8
  • 18

2 Answers2

4

there are two issues with what you are doing here,

the first problem is that preg_replace accept the third parameter as a String , you may need to pass the file content to this as a string

the second problem is that you are trying to require your file as a HTTP call, you need rather to get the file content, actually by calling the link itself you are trying to get the rendered output.

$content = file_get_contents("path/to/file"); // not a file link
$removedPhptags = preg_replace('/^<\?php(.*)(\?>)?$/s', '$1', $content);
hassan
  • 7,812
  • 2
  • 25
  • 36
  • i have tested but i am not get anything..not working – saranchel n Nov 02 '17 at 08:23
  • try to var_dump your $content variable and check what's happening there – hassan Nov 02 '17 at 08:27
  • `$content = file_get_contents("test.php");` – hassan Nov 02 '17 at 08:47
  • Thank you so much Hassan. but one more help i have included one more php file inside menulink.php this file also contaning same like that data how c we can get including file also? please see my edit code. – saranchel n Nov 02 '17 at 08:51
  • short answer: in a direct way NO. but as a work around you may make a use of the [get_included_files](http://php.net/manual/en/function.get-included-files.php) function in PHP. – hassan Nov 02 '17 at 09:00
  • Hi Hassan..i am new in php so please help me how we can use that get_included_files funtion to get all the href details. can you send your perfect code by jsfiddle? – saranchel n Nov 02 '17 at 09:03
  • you need to get the [`included`](http://php.net/include) or the files that `href`ed in your HTML page? – hassan Nov 02 '17 at 09:09
  • that files href in my html page.... means i want to get included php file's html href link also i need – saranchel n Nov 02 '17 at 09:15
  • you can not, read more about the [href](https://html.com/attributes/a-href/) attribute meaning, also have a look at the [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – hassan Nov 02 '17 at 09:18
  • i want to get html content of included php file also because menulink.php file also have if else condition rite? – saranchel n Nov 02 '17 at 09:23
  • See my code..not working. $content = file_get_contents("./test.php"); $included_files = get_included_files($content); foreach ($included_files as $filename) { $content = file_get_contents($filename); } $removedPhptags = preg_replace('/^<\?php(.*)(\?>)?$/s', '$1', $content); echo $content; – saranchel n Nov 02 '17 at 09:25
2

Apart from the answer already given by @hassan, your regular expression will not work:

  • You should not link the opening php tag to the start of the string.
  • You don't want a greedy match as that will take everything from the first opening tag to the last closing tag. Assuming that is what you need.

So all you need as far as the regular expression goes is:

<\?php(.*?)\?>
^            ^ not bound to start nor end (no ^ and $)
         ^ lazy matching, taking as few characters as possible

An example.

jeroen
  • 91,079
  • 21
  • 114
  • 132