0

I have this code: if (fwrite($handle,"

# $hostname.$domainname
add username $hostname.$domainname {$username}
add password $hostname.$domainname {$password}
add autoenable $hostname.$domainname {$autoenable}
add method $hostname.$domainname {$method}
add device $hostname.$domainname {$devicetype}
add encryption $hostname.$domainname {$encrypt}
add userprompt $hostname.$domainname {\"User Name:\"}
"     ) 

I need the output to actually keep the curly braces in the file I am writing to, so the output would for example look like this:

#host123.yahoo.com
add username host123.yahoo.com {test}
add password host123.yahoo.com {testpass}
add autoenable host123.yahoo.com {1}
add method host123.yahoo.com {ssh}
add device host123.yahoo.com {cisco}
add encryption host123.yahoo.com {3des}
add userprompt host123.yahoo.com {\"User Name:\"}

I have tried using double quotes and then single quotes to try and keep the braces, tried escape character \ and spent a lot of time online looking all over. From what I have read, you cannot escape a Curly brace before a variable. Confused on how the syntax should be.

ANy help would be awesome.

2 Answers2

0

You can use double curly braces to escape them, like this:

$somevar = 'fomp fomp';
$text = "This is a test of {{$somevar}}";
echo $text;
// "This is a test of {fomp fomp}"
Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
0

By changing your quotes to ' instead of ", this will enable you to use { inside your command. With the simple use of concatenation, you can insert your variables into the text with ease. Find an example below:

    $InsertDomainName = $hostname.$domainname;
if (fwrite($handle,'
    # '.$InsertDomainName.'
    add username '.$InsertDomainName.' {'.$username.'}
    '   
))
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • I tried this and it worked too, but I went with the answer above only because it was a little easier and cleaner looking. Thanks for the help. – BShifflett Aug 22 '15 at 06:09