I am trying to extract php code from a php file (php file also contains html, css and javascript code). I am using the following regex for this
but this doesn't cater quotation marks (single and double quotes) and comments, i mean how can i skip php tags inside a string (and comments). Please have a look at the following code
I need a regular expression that extracts the two blocks of PHP code from sample code
Code:
<\?[\w\W]*?\?>
Code:
<?php
include("db.php");
$name=$_REQUEST['name'];
/* the regular expression to extract code inside php tags (i.e <? and ?>) is */
$str='|<\?[\w\W]*?\?>|';
# The regex can also be written using double quotes
$str="|<\?[\w\W]*?\?>|";
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample PHP File</title>
</head>
<body>
<?="<h1>Some output from PHP?>
</body>
</html>
Comment