create textbox in php - php

Is there any way to create a textbox of predefined width and height in php memory and store the text in it with word-wrapping, then return it line by line and store in array of variable.
I am actually trying to implement something similar to imagettfbbox but instead of returning dimensions, I want to return text in it line by line; so I could know that on which line and what place particular text or sentence would be after transforming into image.
Please share your ideas.

You might use chunk_split() to wrap a long string and then 'explode' the string to an array, like this:
$mylongstring = 'a very long text etc';
// Place a 'newline' character every 50 characters
$mylongwrappedstring = chunk_split($mylongstring, 50);
// Split the lines to an array
$lineArray = explode("\n", $mylongwrappedstring);
http://php.net/manual/en/function.chunk-split.php
I'll leave the image-processing/generation to you :)

Related

PHP Shuffle Text File and Rewrite Using File_Put_Contents

I've been trying for two days to create an array from a text file list, shuffle it, then rewrite the text file using file_put_contents.
I've succeeded in doing so BUT when I run the script more than once it creates multiple and random spaces between each item.
I've tried many different ideas but no joy yet.
Below is my code.
<?php
// create array from text file
$array = file('list.txt');
// shuffle the array
shuffle ($array);
// overwrite original with new shuffled text file
file_put_contents('list.txt', implode(PHP_EOL, $array));
?>
It seem like you may have extra line returns. These would also be part of the array and subject to random shuffling. IF you want to remove them you can do this
$array = array_filter(array_map('trim', $array));
The array map trim just runs trim() on every item in the array, this will help get rid of lines that have spaces in them instead of being strictly empty. Trim simply removes any whitespace from the front and back of a string, in the case that the whole line is whitespace then all the whitespace is removed leaving an empty string '' for that line.
The array filter will remove any lines that evaluate out to 0 in php this is '' 0 false null and maybe a few other things.
So by combining them we can easily filter out any lines that contain only whitespace or are empty.

How can I only get the string from array that contains images using substr function

I try to cut a string from array to a specified length with PHP substr() function, but my problem is, in some cases, that array only contains images with no single string content on it.
My code:
<div class="ct f-light">
<?php
$num_char = 255;
$text = $in['content'];
echo substr($text, 0, $num_char) . '...';
?>
</div>
$in['content'] is an html. it was processed from CKEditor whichs contain both text and images.
This is the result if the array only contains a string:
and this is the result if it only contains images :
How can I solve this?
What does your $in['content'] look like? Is it in html? Or how did you mix images and text?
If you have that, I would suggest separating them. (into different variables) This would be the cleanest option. Alternatively, you can also search for the first image tag and then only use the text before that, like this:
$num_char = 255;
$text = explode('<img', $in['content'], 2)[0];
echo substr($text, 0, $num_char) . '...';
You then might also want to check for an empty string.
If you are not using html, please clarify what you are using.
(Also, in case you didn't notice, tags like <p> will count towards the 255 character limit, so there will most likely be less than 255 characters visible. Also this might result in bad (incomplete) html.)

HTML textarea alignment

Suppose I have textarea filled with following text
employee/company/salary
john/microsoft/12.000
michael/citrusdata/15.000
How can I align each column vertically so I get following text:
employee__________company__________salary
john______________microsoft__________12.000
michael___________citrusdata__________15.000
In this example I used underscores to specify whitespaces, thought to write a simple function like nl2br() to replace '/' with one or many tab characters but it wont be a consistent solution, guess I need to read text line by line and considering the length of every word, I need to replace '/' with enough whitespace but dont have any idea how to code it, is there any other way?
I suppose you will output the textarea content outside the textarea itself, else you will need to use js alternative. My answer uses php :)
So, you may use the sprintf function that allows left or right padding.
Just split your content to get an array of lines
$lines = explode("\n", $content);
Take care of a eventual empty last entry (if your content end with a \n)
Then
foreach($lines as $line) {
$items = explode("/", $line) ;
echo sprintf("%-15s%-15s%-15s", $items[0], $items[1], $items[2]) . "<br/>";
}
"%-15" tells to left-pad with 15 empty spaces.
It works on console, but you have to nl2br it before echoing in web pages !
This is sample, so you have to add error testing (lines with only one / for example).
You should specify the width of each column like 50 characters for each or any desired width. let say it $COLUMN_WIDTH = 100;
find length of the column value (string) than subtract it from fixed length like
$COUNT_SPACES_TO_INSERT = $COLUMN_WIDTH - strlen($COLUMN_STR);
Than insert $COUNT_SPACES_TO_INSERT number of spaces it will solve your issue.

Html2pdf doesn't support word-break:break-all css

hai everybody i am using html2pdf ,it doesn't support word-break:break-all css any idea?
example
<td style="width:30%;word-break:break-all ;">
testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
</td>
output pdf take above 30% width like string length size
output pdf: testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
I want Output :
testtestetstetstetstetstettstets tetstetstetstetstetstetstetstets
Well, that's complicated. Your teststring is too long, but it's not composed of multiple words. That means that word-break won't work, because there aren't any words to break on. Obviously, this might well just be an example, in which case it might be that html2pdf just doesn't support relative widths and word-break, so you could try having an absolute width and word-break.
That said, here's something I know that will work: wordwrap in PHP. So, instead of echo $yourvar; you could use echo wordwrap($yourvar, 75, "\n", true) instead, which will always cut the string, even if it's just one long string. It takes a little fiddling to get the number of characters to match up with the width that you're looking for, but it will work.
<?php
$foo = str_repeat('test',12);
echo wordwrap($foo, 20, '<br />', true);
Output:
testtesttesttesttest
testtesttesttesttest
testtest
try this;
<td style="width:30%; word-wrap:break-word;">
testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
</td>
not word-break it is word-wrap ;
If you want long strings to wrap consistently within a boundary container I think you should be able to accomplish this by inserting zero-width space characters (​ or \xe2\x80\x8b) between every letter of the orignial string. This will have the effect of wrapping as if every character was its own word, but without displaying the spaces to the end user. This may cause you trouble with text searches or indexing on the final product, but it should accomplish the task reliably from an aesthetic perspective.
Thus:
testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
Becomes
t​e​s​t​t​e​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s
(which displays: "t​e​s​t​t​e​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s")
So if you wrap it it will wrap exactly to the bounds of its container. Here's a fiddle of it as an example.
Just write a PHP script to loop though the string and insert the space:
$string="testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets";
$new_string = "";
for($i=0;$i<strlen($string);$i++){
if ($string[$i]==' ' || $string[$i+1]==' '){ //if it is a space or the next letter is a space, there's no reason to add a break character
continue;
}
$new_string .= $string[$i]."​";
}
echo $new_string
This is a particularly nice solution, because unlike wordwrap(), it automatically adjusts for non-fixed-width fonts (which is basically 99% of fonts that are actually used).
Again, if you need to resulting PDF to be searchable, this is not a good approach, but it will make it look like you want it to.
In your testing the word break will not work because the word break only works between the words in a particular sentence. So yo can use the multiple word sentence and then try with the word breaker
You just use substr function in your code.
I put a example for this. First put your output in variable.
$get_value = "testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);
and so on.
You can use "\r\n" to print newline character. make sure to use it with double quote. If your string is in the variable then you need to use word count function and append this string. You can also use PHP_EOL to avoid platform dependency.
html2pdf does not support this word-break:break-all css
Ref: http://www.yaronet.com/en/posts.php?sl=&h=0&s=151321#0
You may use this method.
<?php
$get_value = "testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);
$third = substr("$get_value",8,11);
?>
I want to add little bit of own experience with HTML2PDF and tables.
I used this solution to generate the PDF containing a table filled with delivery confirmation (list of products). Such list may contain up to thousand of products (rows).
I encountered a problem with formatting and long strings in cells. First problem was that the table was getting too wide even if I set the table's width to 100% and the width of header (<th>) columns (HTML2PDF does not support <colgroup> so I couldn't define it globally) - some columns were out of visible area. I used wordwrap() with <br /> as separator to break down the long strings which looked like it's working. Unfortunately, it turned out that if there is such long string in first and last row the whole table is prepended and appended with empty page. Not a real bugger but doesn't look nice either. The final solution was to (applies for tables which width could outreach the visible area):
set the fixed widths of table and each row in pixels
for A4 letter size I am using total width of 550 px with default margins but you'd have to play around a little to distribute the width between columns
in wordwrap use empty space or ​ / \xe2\x80\x8b as delimiter
For small tables that you'd like to spread for 100% of visible area width it is OK to use width expressed in %.
I think this function is a limping solution.
function String2PDFString($string,$Length)
{
$Arry=explode(" ",$string);
foreach($Arry as $Line)
{
if(strlen($Line)>$Length)
$NewString.=wordwrap ($Line,$Length," ",true);
else
$NewString.=" ".$Line;
}
return $NewString;
}

automatic line-break of textarea inside a form possible with PHP?

I know about the nl2br function... But is there a way to set a maximum length of a line?
Im using php to get results from a form, which contains a textarea.
I dont want words to get broken to newlines, like the word 'example' to become 'example'
Thanks
You can use wordwrap to force a line break:
wordwrap($str, 123, "<br>\n", true)
You might be interested in the wordwrap function in php.

Categories