Jump to content

Recommended Posts

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 :confused:

 

Link to comment
https://forums.phpfreaks.com/topic/238313-nowdoc-parsing-problem/
Share on other sites

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.

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

 

 

 

 

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.

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.

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 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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.