I'm trying to combine several php files in to one larger with php.
I'm sending form data to a small files and use includes to load into them in a
main page.
With many of these files each time I work an inspection project is getting to be a little time consuming dropping to a command prompt.
I'm able to combine pages with a command prompt using the "cat" command and pull the ones not yet completed.
cat page-1.php page-2.php page-3.php page-4.php page-5.php page-6.php page-7.php page-8.php page-9.php page-10.php page-11.php page-12.php >> newpage.php
I need help with php to perform this with out having to execute a shell command from php.
I also need the code to ignore missing files and continue with the rest.
Thanks in advance
foreach (glob("*.php") as $filename) {
$code.=file_get_contents("./$filename");
}
file_put_contents("./combined.php",$code);
Run that in your target directory. That reads all your php files in the directory, extracts their code and then combines and saves it in your new file.
This will keep them in the order they need to be. The other way does not keep them in order and will display errors.
<?php
$txt1 = file_get_contents('page-001.php');
$txt1 .= "\n" . file_get_contents('page-002.php');
$txt1 .= "\n" . file_get_contents('page-003.php');
$txt1 .= "\n" . file_get_contents('page-004.php');
$txt1 .= "\n" . file_get_contents('page-005.php');
$txt1 .= "\n" . file_get_contents('page-006.php');
$txt1 .= "\n" . file_get_contents('page-007.php');
$txt1 .= "\n" . file_get_contents('page-008.php');
$txt1 .= "\n" . file_get_contents('page-009.php');
$txt1 .= "\n" . file_get_contents('page-010.php');
$txt1 .= "\n" . file_get_contents('page-011.php');
$txt1 .= "\n" . file_get_contents('page-012.php');
$fp = fopen('newcombined.php', 'w');
if(!$fp)
die('Could not create / open text file for writing.');
if(fwrite($fp, $txt1) === false)
die('Could not write to text file.');
echo 'Text files have been merged.';
?>
Related
I'm trying to build a widget for a website that displays a random background image everytime the page is refreshed and i'm running into some trouble with string concatenation.
$display .= "<p class=\"ey-image-quote\" style\"background-image:url('".$img."')\">" . $this->quote . "</p>";
This returns a url without slashes (replaced by spaces) and i have not found a viable solution yet.
Is there any way to achieve this in php?
$display .= "<p class=\"ey-image-quote\" style=\"background-image:url('".$img."')\">" . $this->quote . "</p>";
You forgot = in style
Im working on a project where I receive a webhook of JSON data that I will then send an email notification based on the JSON. I have all the sorting of the JSON worked out. I know how to send an HTML email via php, but I'm lost on how to build such a large email.
I will need to use some logic to build out parts of the HTML email. But I know something like this will fail because I'm trying to use an If statement inside a variable...
$email = $vendorEmail;
$email_subject = "Order Slip ";
$email_message = '
<table style="width:100%;">
<tbody>
<tr>
<td style="width:33%;">
<h5>Bill To:</h5>
<p style="font-size: 14px;">
<strong>' . $orderInfo->billing_address->first_name . ' ' . $orderInfo->billing_address->last_name . '</strong><br/>
' . if(isset($orderInfo->billing_address->company)){$orderInfo->billing_address->company };. '<br>
' . $orderInfo->billing_address->address1 . '<br/>
' . $orderInfo->billing_address->address2 . '<br/>
</p>
</td>
This is a small section of my overall email. The logic will become much more complex further in the email. An example would be running a for statement to run through all the lines items of a completed order.
Is there a standard way of creating larger more complex HTML emails? Or if not, Does anyone have a suggestion on the smarter way to go about this?
Your question is not about composing emails, but about combining larger strings - it might help to rename the question better so it attracts the "correct" answers.
If your only problem is using if/else inside a variable you can use two approaches.
First uses normal if/else clause with adding to a variable using the .= operator
$string = 'xxx'
if($a) {
$string .= 'yyy';
}
else {
$string .= 'zzz';
}
OR use ternary operators to straightforward define the variable in one go. Output of the second option is exactly the same as the first one.
$string = 'xxx' . ($a ? 'yyy' : 'zzz');
Ofc you can combine these two approaches.
More about ternary operators: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
EDIT:
to put it into context, your code should look like this (ternary approach which I would recommend in this piece of code)
$email = $vendorEmail;
$email_subject = "Order Slip ";
$email_message = '
<table style="width:100%;">
<tbody>
<tr>
<td style="width:33%;">
<h5>Bill To:</h5>
<p style="font-size: 14px;">
<strong>' . $orderInfo->billing_address->first_name . ' ' . $orderInfo->billing_address->last_name . '</strong><br/>
' . ((isset($orderInfo->billing_address->company) ? $orderInfo->billing_address->company : NULL). '<br>
' . $orderInfo->billing_address->address1 . '<br/>
' . $orderInfo->billing_address->address2 . '<br/>
</p>
</td>
EDIT2:
When dealing with really large HTML strings I also use output buffering approach, you might find it useful too.
<?php
ob_start();
$phpVariables = 'phpVariables';
?>
HERE GOES LARGE HTML, <?=$phpVariables?>, or <?php echo 'chunks of PHP that output stuff'?>
<?php
$string = ob_get_clean();
?>
$string would then be "HERE GOES LARGE HTML, phpVariables, or chunks of PHP that output stuff.".
Of course you can use normal php code, including your if/else in the output.
If you want to use that approach though, I suggest you first get familiar with output buffering http://us2.php.net/manual/en/function.ob-start.php
You can use if statements in your large strings but using the shorter version, example:
echo "-text-" . ▼ ▼
"Name: " . ( ( isset( $name ) ) ? $name : "" ) .
"-text-";
When using this "short if" remember to enclose it all in parenthesis (pointed by arrows ▼). Now, in your code, replace your current if :
if(isset($orderInfo->billing_address->company)){$orderInfo->billing_address->company }; .
by :
( (isset($orderInfo->billing_address->company)) ? $orderInfo->billing_address->company : "" ) .
I have the following code:
$markupimage = substr($product_info['image'],0,strrpos($product_info['image'],".")) . "-" . $popw . "x" . $poph . substr($product_info['image'],strrpos($product_info['image'],"."));
this currently outputs with $markupimage =:
http://shop.example.com/image/cache/data/NEW WEBSITE/Products/Hangers/Tees/Full Body/front_black-1000x1000.jpg
Where the 'NEW WEBSITE' text is, the php has missed out the url encoding. It should be NEW20%WEBSITE. How can I alter the php script to include this url encoding?
EDIT:
This is where I use this php script:
$this->document->setFBOG('og:image', HTTP_SERVER . 'image/cache/' . $markupimage);
I have tried wrapping this in both the rawurlencode() and urlencode()
So far I have tried:
$this->document->setFBOG('og:image', urlencode(HTTP_SERVER . 'image/cache/' . $markupimage));
$this->document->setFBOG('og:image', rawurlencode(HTTP_SERVER . 'image/cache/' . $markupimage));
This results in a link that looks something like:
http://shop.example.com/image/cache/data%2FNEW+WEBSITE%2FProducts%2FHangers%2FTees%2FFull+Body%2Ffront_black-1000x1000.jpg
You can use urlencode() function for that.
urlencode()
I'm using fwrite() to write a file, however, every time it runs, it prints all of the information to the webpage it's on and writes to the file, too. I'd rather it not echo it to the page.
Alright, I've retrieved the code. Here it is:
fwrite($data_file, "Confirmation Data: \r\n\r\n Song: " . $input_song . "\r\nFile1: " . $file_var1 . "\r\nFile2: " . $file_var2 . "\r\nFile3: " . $file_var3 . "\r\nFile4: " . $file_var4 . "\r\nExplanation Text: " . $input_explanation);
Whenever I run this, it outputs everything in the second place of fwrite() onto the page, so it outputs the follow onto the page, except with the variables replaced for their values:
Confirmation Data: \r\n\r\n Song: " . $input_song . "\r\nFile1: " . $file_var1 . "\r\nFile2: " . $file_var2 . "\r\nFile3: " . $file_var3 . "\r\nFile4: " . $file_var4 . "\r\nExplanation Text: " . $input_explanation
You should post your code, so we can follow it but most probably you are also echoing out the code as HTML in your PHP file and this is why it prints out on the screen.
You said your information is being echoed and not written to file. Perhaps you are using echo instead of using file related functions. However, without access to your code, I can only post how to write information to a file in PHP:
<?php
error_reporting(E_ALL);//display all errors
ini_set('display_errors', true);
$filename = "testfile.txt";//the name of your output file
$fh = fopen($myFile, 'w');//create a filehandler - fh) and open the file in write mode
$string = "ABCDEFGH\n";//test string to write to the file
$myvar = fwrite($fh, $string);
$string = "IJKLMNO\n";//update the string variable with new data
fwrite($fh, $string);//write more data - can be repeated ad -infinitum
fclose($fh);//finally, close the file
But, if you truly want definitive answers, post your code so that we may be able to help you.
I'm using a Custom Wordpress Template which is using the wp_mail Function.
So I have this code below, and the bolded portion is giving me problems:
wp_mail($et_email_to, sprintf( '[%s] ' . esc_html($_POST['et_contact_subject']), $et_site_name ), esc_html($_POST['et_contact_message']),'From: "'. esc_html($_POST['et_contact_name']) .'" <' . esc_html($_POST['et_contact_email']) . '>');
I want to include extra stuff where the Message portion is. esc_html($_POST['et_contact_message'])
When I try to add anything extra there, it gives various PHP errors.
If I replace that esc_html() part with normal text like '12345', it works fine.
What I want to add to the end of my messages is this:
"IP Address: ".$_SERVER['REMOTE_ADDR']."
Came From: ".$_SERVER['HTTP_REFERER']."
Browser: ".$_SERVER['HTTP_USER_AGENT']
It works fine if I remove that esc_html() portion, but I can't get both to work together.
esc_html() works fine by itself also, it prints the Message from the Contact form.
I just can't get both things to work together.
These will fail for example:
esc_html($_POST['et_contact_message']) 12345,
esc_html($_POST['et_contact_message'] $_SERVER['REMOTE_ADDR']),
etc...
Please help me out here.
Thanks!
Check out string concatenation in PHP - there are many ways to do it, I'll highlight a few from http://www.php.net/manual/en/language.types.string.php
<?php
$foo = 'some';
$bar = 'baz';
$message = '';
$message .= 'Add ' . $foo;
$message .= ' variable to a string';
// You can do this all at once, in one line or many
$mesage = 'Add ' . $foo . ' variable to a string';
// Double-quotes are useful
$message = "Add {$foo} variable to a string";
// And there are other methods, like HEREDOC
$message = <<<TEXT
Add $foo variable to a string...in a HEREDOC
block.
TEXT;
// Some editors, like VIM, support syntax highlighting in HEREDOC
// Useful if you want to write out legeble code from other languages, like
// SQL.
$message = <<<SQL
SELECT
*
FROM
table
WHERE
col = %s
SQL;
$wpdb->prepare($message, $foo);
I finally found a way to make it work!
Here is the code:
$message = $_POST['et_contact_message'];
$message .= '
IP Address: ' . $_SERVER['REMOTE_ADDR'];
$message .= '
Came From: ' . $_SERVER['HTTP_REFERER'];
$message .= '
Browser: ' . $_SERVER['HTTP_USER_AGENT'];
wp_mail($et_email_to, sprintf( '[%s] ' . esc_html($_POST['et_contact_subject']), $et_site_name ), $message,'From: "'. esc_html($_POST['et_contact_name']) .'" <' . esc_html($_POST['et_contact_email']) . '>');