View Full Version : Using PHP tags in arguements, and variables

05-01-2006, 11:18 PM
I'm getting some odd results when using <?php or ?> in a script.

What I am doing is parsing a PHP/HTML file, and passing the segments to a form via a hidden input. I don't want to execute the code, just pass it on for inclusion in a file.

My question is this: Can I use the PHP open and or close tags in function arguements, or variables, without breaking the code? My guess was no, but the parsing "seems" to work, but when it goes to the form I get odd stuff. So I'm wondering if I'm just plain trying to do a no-no here.

Example (breaks file into PHP and HTML pieces, then passes pieces to a form for editing, recombining and saving):

preg_match_all("|(<\?php.*\?>)(.*)(<\?php.*\?>)|s", $contents, $matches);
$head = nl2br(htmlentities($matches[1][0]));
//thus $head is something like <?php some stuff ?>


<input type="hidden" name="head" value="$head" />

So is using raw PHP open/close tags in an argument, or passing passing them in a variable not possible/good practice?


05-02-2006, 11:20 AM
you could bypass this potential problem by just making your variable (that contains the string representation of your code) a global variable...then you don;t need to pass it into your function via the argument...

Doing it this way, as long as the PHP code is inside a variable string, you should be fine...

Passing the data into the fumction via the argument is the prefered method, but if it doesn;t work, you could try doing what i said here...

ralph l mayo
05-02-2006, 05:34 PM
I think you're getting odd stuff because your regular expression is wrong. Shouldn't some or all of those matches be lazy so they don't swallow up the whole document? Try:

preg_match_all('/<\?php(.*?)\?>/i', $phpstring, $matches);

$matches[0] will contain full php blocks and $matches[1] will just have the contents, minus the delimiting tags.