file_put_contents and a new line and space - file-put-contents

After sending the string via the email i want to save it into file.
so i do it this way:
file_put_contents("coupon.txt", $email. $RandomString , FILE_APPEND | LOCK_EX);
and in this way it's look like this:
email#email.com12d45f8e9email2#email2.com1d2d5s4d51d
so no new line and no space between the codes.
when i tray to use this way:
file_put_contents("coupon.txt", $email. . $RandomString .PHP_EOL. , FILE_APPEND | LOCK_EX);
it's just wont save anything, more then this is to error the mail sending.
Tal.

use \t and \n in the string you want to write.
file_put_contents("coupon.txt", $email."\t". $RandomString . "\n" , FILE_APPEND | LOCK_EX);

You need to append "\n", try this:
file_put_contents("coupon.txt", $email . " " . $RandomString . "\n" , FILE_APPEND | LOCK_EX);

Related

automatically generate tags from content in wordpress

my WordPress website contents have some words in every line,
I want a code to automatically convert every line in content to tag (every line not every word), let's say my content is like this:
Beagle puppy
Costumes
Wales
Dogs
I want tags from every line: Beagle puppy, Costumes, Wales, Dogs
I don't want to use plugins, because I used some but it needs a keyword list to match the content. I don't want to use any keyword list to match the content.
is it possible to convert every line in content into one tag?
Assuming you meant what you appeared to mean - for instance, that the source post consists solely of the lines to be converted, the lines won't include additional odd/inappropriate chracters to be handled, and so on - the following will work. If you need to convert a section of a post or some other element defined in some other way, or attach the lines as tags to the current post, etc., then you'll need to provide those details clearly.
Place shortcode [convert_post_lines_to_tags] in new output post.
Save draft, and preview (shortcode won't function yet, obviously)
Add functions to your theme functions.php
Provide "$source_post_id" where indicated.
Re-load output post
add_shortcode( 'convert_post_lines_to_tags', 'convert_post_lines_to_tags' ) ;
function convert_post_lines_to_tags() {
$source_post_id = '' ; //Provide ID Number of post with lines to be converted
$i = 0 ;
$newTags = 'New tags inserted: <br />' ;
//TIL - PHP requires double quotes to replace escaped characters
$post_content = str_replace(
array( "\r\n", "\r" ), ',', get_post( $source_post_id )->post_content
) ;
$post_line_array = explode( ',', $post_content ) ;
foreach ( $post_line_array as $line_tag ) {
$tag = wp_insert_term( $line_tag, 'post_tag' ) ;
if ( ! is_wp_error( $tag ) ) {
$i++ ;
$newTags .= $i . '. ' . get_term( $tag['term_id'] )->name . '<br />' ;
}
}
return $newTags ;
}

TYPO3 Extbase FE-Extension plugin failing with: PHP Warning: rawurlencode() expects parameter 1 to be string, object given

This question is based on my previous one where my entire TYPO3 website didn't work.
Now, after adjusting the php-version (5.6.17) the website itself works, but one fe-plugin of my extbase extension doesn't work - even though it's identical to the one on a copy of the website where everything works. The other fe-plugin from the same extension directly worked out of the box.
I get the following error in the frontend whenever I call a page that contains this plugin and I don't know where to start searching for the cause.
(I changed my domain to <mydomain> and my plugin name to tx_myfeplugin_nameexte in the following error snippet):
#1: PHP Warning: rawurlencode() expects parameter 1 to be string, object given in /var/www/vhosts/<my-domain>/typo3/sysext/core/Classes/Utility/GeneralUtility.php line 1641 (More information)
TYPO3\CMS\Core\Error\Exception thrown in file
/var/www/vhosts/<my-domain>/typo3/sysext/core/Classes/Error/ErrorHandler.php in line 101.
49 TYPO3\CMS\Core\Error\ErrorHandler::handleError(2, "rawurlencode() expects parameter 1 to be string, object given", "/var/www/vhosts/<my-domain>…po3/sysext/core/Classes/Utility/GeneralUtility.php", 1641, array)
48 rawurlencode(TYPO3\CMS\Extbase\Persistence\Generic\QueryResult)
/var/www/vhosts/<my-domain>/typo3/sysext/core/Classes/Utility/GeneralUtility.php:
01639: } else {
01640: if (!$skipBlank || (string)$AVal !== '') {
01641: $str .= '&' . ($rawurlencodeParamName ? rawurlencode($thisKeyName) : $thisKeyName) . '=' . rawurlencode($AVal);
01642: }
01643: }
47 TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl("tx_myfeplugin_nameexte", array, "", boolean, boolean)
/var/www/vhosts/<my-domain>/typo3/sysext/core/Classes/Utility/GeneralUtility.php:
01636: $thisKeyName = $name ? $name . '[' . $Akey . ']' : $Akey;
01637: if (is_array($AVal)) {
01638: $str = self::implodeArrayForUrl($thisKeyName, $AVal, $str, $skipBlank, $rawurlencodeParamName);
01639: } else {
01640: if (!$skipBlank || (string)$AVal !== '') {
Did someone have the same error before or has an idea what I should try to do to fix this?
Thanks to the answer I guess I fixed it, because the error is not appearing any longer, by adding the following lines at line 1707 in file GeneralUtility.php:
if ($AVal instanceof \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult) {
$AVal = $AVal->toArray();
}
lets have a look to the source:
foreach ($theArray as $Akey => $AVal) {
$thisKeyName = $name ? $name . '[' . $Akey . ']' : $Akey;
if (is_array($AVal)) {
$str = self::implodeArrayForUrl($thisKeyName, $AVal, $str, $skipBlank, $rawurlencodeParamName);
} else {
if (!$skipBlank || (string)$AVal !== '') {
$str .= '&' . ($rawurlencodeParamName ? rawurlencode($thisKeyName) : $thisKeyName . '=' . rawurlencode($AVal);
}
}
}
The array you give has to be a multidimensional array as it represents the parts of the url. Every element is testet being an array, so you could debug $AVal being an object but an array.
I guess that there could be an stdObject from any conversion you made before. Debugging will help you.
Second, what is the reporting you set in the install tool. Set it to production, will ist work then?

How can I read Excel files with symfony2?

I installed Excelbundle with PhpExcel library. I want to read excel files I found this function.
How can I use it? Any suggestion?
public function xlsAction()
{
$filenames = "your-file-name";
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filenames);
foreach ($phpExcelObject ->getWorksheetIterator() as $worksheet) {
echo 'Worksheet - ' , $worksheet->getTitle();
foreach ($worksheet->getRowIterator() as $row) {
echo ' Row number - ' , $row->getRowIndex();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
echo ' Cell - ' , $cell->getCoordinate() , ' - ' , $cell->getCalculatedValue();
}
}
}
}
}
My suggestion is "Read the documentation" and start hacking at it. Working with excel is, in my experience, quite complex and time consuming so don't expect other people to solve your problem online.
It seems like you're talking about this bundle:
https://github.com/liuggio/ExcelBundle
It has great documentation, even full examples (see "Fake Controller").
With PHPExcel it is quite easy to read a Excel document.
See my example :
$dir = $this->getContainer()->getParameter("kernel.root_dir") . "/../../data/import/";
$file_name = "my_excel_file.xlsx";
$excel = $this->getContainer()->get('phpexcel')->createPHPExcelObject($dir . DIRECTORY_SEPARATOR . $file_name);
$sheet = $excel->getActiveSheet();
$row = 0;
while ($sheet->getCellByColumnAndRow(1, $row)->getValue()) {
$data = $sheet->getCellByColumnAndRow(2, $row)->getValue(); // get value from nth line and 2nf column
//do stuff -- see doc for functions on $sheet
}

How use Send() if the content has "" in it ? (autoIt)

Run("cmd.exe")
Global $String = "wmic process where name="example.exe" get commandline"
Send($String)
Result :
Global $String = "wmic process where name="example.exe" get commandline"
Global $String = "wmic process where name="^ ERROR
You just use single quotes to surround your string.
$string = 'test "example" and more stuff'
Or you can "escape" your double quotes by doubling them like stated in the Strings description of the Datatypes in AutoIt.
$string = "test ""example"" and more stuff"

Rename files during upload within Wordpress

I am trying to rename upload filenames match the Post Title.
This other thread shows how to rename to hash:
Rename files during upload within Wordpress backend
Using this code:
function make_filename_hash($filename) {
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($filename, $ext);
return md5($name) . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);
Does anyone know the code to rename the file to match Post Title.extension?
barakadam's answer is almost correct, just a little correction based on the comment I left below his answer.
function new_filename($filename, $filename_raw) {
global $post;
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$new = $post->post_title . $ext;
// the if is to make sure the script goes into an indefinate loop
if( $new != $filename_raw ) {
$new = sanitize_file_name( $new );
}
return $new;
}
add_filter('sanitize_file_name', 'new_filename', 10, 2);
Explanation of code:
Lets assume you upload a file with the original filename called picture one.jpg to a post called "My Holiday in Paris/London".
When you upload a file, WordPress removes special characters from the original filename using the sanitize_file_name() function.
Right at the bottom of the function is where the filter is.
// line 854 of wp-includes/formatting.php
return apply_filters('sanitize_file_name', $filename, $filename_raw);
At this point, $filename would be picture-one.jpg. Because we used add_filter(), our new_filename() function will be called with $filename as picture-one.jpg and $filename_raw as picture one.jpg.
Our new_filename() function then replaces the filename with the post title with the original extension appended. If we stop here, the new filename $new would end up being My Holiday in Paris/London.jpg which all of us know is an invalid filename.
Here is when we call the sanitize_file_name function again. Note the conditional statement there. Since $new != $filename_raw at this point, it tries to sanitize the filename again.
sanitize_file_name() will be called and at the end of the function, $filename would be My-Holiday-in-Paris-London.jpg while $filename_raw would still be My Holiday in Paris/London.jpg. Because of the apply_filters(), our new_filename() function runs again. But this time, because $new == $filename_raw, thats where it ends.
And My-Holiday-in-Paris-London.jpg is finally returned.
Something like this? (considering $post is your post variable, make it global):
function new_filename($filename) {
global $post;
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
return $post->post_title . $ext;
}
add_filter('sanitize_file_name', 'new_filename', 10);
Did I understand you?

Resources