huntermiki Posted June 3, 2011 Share Posted June 3, 2011 hi i have problem using nowdoc i have some large string data that contain varable inside i have put them on file include.php like this <?php $var1 = <<<'FIRST' the first name is $varb FIRST; and included that in my code now i have something like this <?php include("include.php"); $varb = angry; echo $var1; ?> my problem is that how can i force php to parse varables inner strings need help and tired of googling Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/ Share on other sites More sharing options...
mattal999 Posted June 3, 2011 Share Posted June 3, 2011 As I recall you need to enclose variables in {}, like so: $var1 = <<<FIRST the first name is {$varb} FIRST; Edit: After some manual reading, you need to use a heredoc instead of a nowdoc. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc That says that PHP doesn't parse anything inside a nowdoc, which is defined using single quotes around your name (in this case, FIRST). By removing these quotes everything works as it should. Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224692 Share on other sites More sharing options...
huntermiki Posted June 3, 2011 Author Share Posted June 3, 2011 it seems i didnt explain it good i have a include file with varables of 100 line and near 40 string that are not defined in this include file that if i dont use nowdoc and use heredoc instead i will get error because didnt define the variables inside string and i want to use this variables from include file in many places in code and before inserting or using them i will define that variables indside string i need to define this variables first single quoted and after that in other place in the code first define inner variables value and force php to parse main variable hope i can show this better this way $vara = <<<'END' my first name is {$first_name} END; in this part of code i didnt define $first_name so if i dont use nowdoc this will bring this error Notice: Undefined variable: first_name now in the main code i will include above code and then define first name and then i need to `vara` parse and replace `$first_one` part of string with its curent value mean // include above file and then $first_name = "tired"; echo $vara ; that should out some thing like this my name is tired i want to know is there any way for parsing inner varables on nowdoc Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224715 Share on other sites More sharing options...
mattal999 Posted June 3, 2011 Share Posted June 3, 2011 I imagine a simple str_replace would suffice. <?php $vara = <<<'END' my first name is $first_name END; $variables['first_name'] = "james"; foreach($variables as $name => $value) { $vara = str_replace("$".$name, $value, $vara); } echo $vara; // Output: my first name is james ?> That's the best I can come up with. You may be able to use regular expressions to pick the variable names out of the nowdoc after including the defining file and then replace them with the actual variable names. Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224723 Share on other sites More sharing options...
huntermiki Posted June 3, 2011 Author Share Posted June 3, 2011 thanks its really good answer tanks it solved i think Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224725 Share on other sites More sharing options...
mattal999 Posted June 3, 2011 Share Posted June 3, 2011 Got a better solution after twiddling with one of my Twitter regular expressions. <?php $vara = <<<'END' my first name is $first_name END; $first_name = "james"; preg_match_all("/ +\\$(.*)?/i", $vara, $matches); foreach($matches[1] as $match) { $vara = str_replace("$" . $match, $$match, $vara); } echo $vara; ?> With that, you literally just define all of your variables as you do at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224729 Share on other sites More sharing options...
jcbones Posted June 3, 2011 Share Posted June 3, 2011 I don't understand why you can't define the variables, then include the heredoc. Which would parse all of the variables that have been defined. Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224760 Share on other sites More sharing options...
wildteen88 Posted June 3, 2011 Share Posted June 3, 2011 I don't understand why you can't define the variables, then include the heredoc. Which would parse all of the variables that have been defined. You can do that. Just make sure the heredoc statement is included after you have defined the variables. Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224813 Share on other sites More sharing options...
jcbones Posted June 4, 2011 Share Posted June 4, 2011 I don't understand why you can't define the variables, then include the heredoc. Which would parse all of the variables that have been defined. You can do that. Just make sure the heredoc statement is included after you have defined the variables. I know I can, just don't understand why HE can't. Quote Link to comment https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/#findComment-1224971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.