TCPDF Justification Issue with Last Line of Paragraph - php

I am using the PHP Library TCPDF. I am running into a small issue when using the MultiCell method and justification option. The last line of the paragraph justifies (I suppose does what it is supposed to) but only with a few words leaving large spaces in between. These are dynamic forms I am creating so MultiCell (instead of text or write) is needed.
Does anyone know if there is a way to prevent this from happening but still be able to use the MultiCell method? I have ran into weird issues in the past using writeHTML for things and want to avoid if possible.
Here is a sample of code producing the result:
$text = 'This is an example paragraph with no other meaning than to show what is currently happening with this justification issue. I really hope there is a way to keep the ';
$text .= 'justification, yet keep the last sentence from doing so and looking silly. Thank you all for your help and time, it is much appreciated.';
$this->_pdf->MultiCell(0, 0, $text, 0, 'J', false, 1);
Thank you very much for your time.

I don't think that there is a 'setting' to handle this; however, if you simply put a new line ("\n") at the end of your string it does exactly what I was looking for.
So if anyone is having a similar problem, the answer (in my case) is to simply add a new line.
$text = "...last line of the paragraph.\n";

Related

Add a FNC1 character to code created with tcpdf datamatrix

I am using tcpdf to generate datamatrix barcodes. Works really nice. Now I was asked if we could add fnc1 characters to our code.
But I have no clue what the representation of the fnc1 character would be correct for the tcpdf generator.
I came across this here http://sourceforge.net/p/tcpdf/discussion/435311/thread/161b1b1a
But I would like to understand where the answer of using chr(241) actually comes from. To me it seems like it fell from the sky. Documentation doesn't say anything about it and I have not found anywhere else that chr(241) would be a representation of the fnc1 character.
Apart from that, it doesn't work for me, scanning the barcode just results in ñ characters in the middle of the code.
Anyone an idea how I could get the fnc1 character into my tcpdf datamatrix? What am I missing? Thanks for help in advance!
$string = chr(241).str_replace(";", chr(241), $string);
$barcodeobj = new TCPDF2DBarcode();
$barcodeobj->setBarcode($string, 'DATAMATRIX');
$barcodeobj->getBarcodeSVGcode(6, 6, 'black');
Looking at the code for version 1.0.008 (from 2014-05-06) in /tcpdf/include/barcodes/datamatrix.php I cannot see any comprehensive treatment of the special function or macro characters in Data Matrix so you are probably out of luck.
That said, the forum reply to which you link was written by the author of the TCPDF (Nicola Asuni) so it might we worth reaching out to him to see what he was thinking at the time. My guess would be that an example input used by some other library had mislead him into believing that FNC1 can be represented as an ordinary code point, however this is wrong since FNC1 is a non-data character that requires special treatment.

Simple regex seems to cause infinite loop in PHP

The following 2 lines are my code:
$rank_content = file_get_contents('https://www.championsofregnum.com/index.php?l=1&ref=gmg&sec=42&world=2');
$tmp_ = preg_replace("/.+width=.16.> /Uis", "", $rank_content, 1);
The second line above causes an infinite loop.
In contrary, the following alternatives DO work:
$tmp_ = preg_replace("/.+width=.16.> /Ui", "", $rank_content, 1);
$tmp_ = preg_replace("/[^§]+width=.16.> /Uis", "", $rank_content, 1);
But sadly, they do not give me what I want - both alternatives do not include line breaks within $rank_content.
Also, if I replaced the file_get_contents function with something like
$rank_content = "asdfas\nasdfasdfaswidth=m16m> teststring";
There are no problems either, although \n represents a line break, too, doesn’t it?!
So do I understand it right that RegEx has problems in noticing a String with line breaks in it?
How can I filter a substring of $rank_content (which has multiple lines in it) by removing some lines until something like "width="16" " appears? (Can be seen in the site's source code)
Replace the m modifier with the s modifier. m changes the behaviour of ^ and $, whereas s changes the behaviour of .
That said, you should not be parsing HTML with regex. Seriously. Bad things happen.
I give up on it: It seems the problem is the LENGTH of the haystack variable $rank_content. Its length is about 90,000, while the maximum allowed length for regex match() is about 30,000, so I guess it is the same for regex replace().
Solving this problem would surely be possible, if somebody is interested: Have a look into this link -> PHP preg_match_all limit
I myself am going to solve the problem using another method for reading the contents of a website like HTML Unit or maybe retrieving the site line after line.

Why should use linebreak '\n'

I need some advice ...
I'd like to know if it is a good practice to use in a code line breaks "\n"? What is the purpose?
$my_string .= "\n" . "<p>Some values</p>" . "\n";
Till now I haven't use it and I'd really like to know ... your opinion.
Thanks
You only need to use "\n" to make the resulting html code neater/easier to read. It is not necessary.
The new line makes your generated code easier to read. You might think noone should read your code, however if you run into some problems the generated code is easier to read for debugging purposes as well.
\n can be used when streaming over sockets as well. Sometimes you need to use \r, depending on the operating system.
This is very common mistake of the newbie programmers.
They never have an idea that the result of the PHP script execution is plain HTML code.
And sometimes a programmer have to sort things out with that HTML.
While it's just impossible if there are no linegreaks in the code.
Anyway, a good practice would be
$my_string = "Some values";
?>
<p><?=$my_string</p>
so, you won't need no special linebreaks in the PHP code.
Also, there are some cases where you have to use linebreaks.
For example, if you are composing an HTML email message, you hve to add linebreaks, or they will be forcibly added in the unexpected places.
at time of writing to break line, means display text after line-break to next line
like enter in some text editor...
The only purpose is for people using the "View Source" feature of the browser - basically no one. While it is considered good practice, I almost never do (because it's pointless) :)
for pre-tag (pre formatted text):- http://webdesign.about.com/od/htmltags/f/blfaqpre.htm
so, you can do this in SO
1
2
3
4
5
6
7
8
\n is just an escape for a new line. An escape is not really required in PHP, but it makes it seamless to output a new line mark.

Removing Break Lines

I've asked this question before but I didn't seem to get the right answer. I've got a problem with new lines in text. Javascript and jQuery don't like things like this:
alert('text
text);
When I pull information from a database table that has a break line in it, JS and jQuery can't parse it correctly. I've been told to use n2lbr(), but that doesn't work when someone uses 'shift+enter' or 'enter' when typing text into a message (which is where I get this problem). I still end up with separate lines when using it. It seems to correctly apply the BR tag after the line break, but it still leaves the break there.
Can anyone provide some help here? I get the message data with jQuery and send it off to PHP file to storage, so I'd like to fix the problem there.
This wouldn't be a problem normally, but I want to pull all of a users messages when they first load up their inbox and then display it to them via jQuery when they select a certain message.
You could use a regexp to replace newlines with spaces:
alert('<?php preg_replace("/[\n\r\f]+/m","<br />", $text); ?>');
The m modifier will match across newlines, which in this case I think is important.
edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.
edit2: like #LainIwakura, I made a mistake in my regexp, partly due to the previous edit. my new regexp only replaces CR/NL/LF characters, not any whitespace character (\s). note there are a bunch of unicode linebreak characters that i haven't acknowledged... if you need to deal with these, you might want to read up on the regexp syntax for unicode
Edit: Okay after much tripping over myself I believe you want this:
$str = preg_replace('/\n+/', '<br />', $str);
And with that I'm going to bed...too late to be answering questions.
I usually use json_encode() to format string for use in JavaScript, as it does everything that's necessary for making JS-valid value.

preg_replace() help in PHP

Consider this string
hello awesome <a href="" rel="external" title="so awesome is cool"> stuff stuff
What regex could I use to match any occurence of awesome which doesn't appear within the title attribute of the anchor?
So far, this is what I've came up with (it doesn't work sadly)
/[^."]*(awesome)[^."]*/i
Edit
I took Alan M's advice and used a regex to capture every word and send it to a callback. Thanks Alan M for your advice. Here is my final code.
$plantDetails = end($this->_model->getPlantById($plantId));
$botany = new Botany_Model();
$this->_botanyWords = $botany->getArray();
foreach($plantDetails as $key=>$detail) {
$detail = preg_replace_callback('/\b[a-z]+\b/iU', array($this, '_processBotanyWords'), $detail);
$plantDetails[$key] = $detail;
}
And the _processBotanyWords()...
private function _processBotanyWords($match) {
$botanyWords = $this->_botanyWords;
$word = $match[0];
if (array_key_exists($word, $botanyWords)) {
return '' . $word . '';
} else {
return $word;
}
}
Hope this well help someone else some day! Thanks again for all your answers.
This subject comes up pretty much every day here and basically the issue is this: you shouldn't be using regular expressions to parse or alter HTML (or XML). That's what HTML/XML parsers are for. The above problem is just one of the issues you'll face. You may get something that mostly works but there'll still be corner cases where it doesn't.
Just use an HTML parser.
Asssuming this is related to the question you posted and deleted a little while ago (that was you, wasn't it?), it's your fundamental approach that's wrong. You said you were generating these HTML links yourself by replacing words from a list of keywords. The trouble is that keywords farther down the list sometimes appear in the generated title attributes and get replaced by mistake--and now you're trying to fix the mistakes.
The underlying problem is that you're replacing each keyword using a separate call to preg_replace, effectively processing the entire text over and over again. What you should do is process the text once, matching every single word and looking it up in your list of keywords; if it's on the list, replace it. I'm not set up to write/test PHP code, but you probably want to use preg_replace_callback:
$text = preg_replace_callback('/\b[A-Za-z]+\b/', "the_callback", $text);
"the_callback" is the name of a function that looks up the word and, if it's in the list, generates the appropriate link; otherwise it returns the matched word. It may sound inefficient, processing every word like this, but in fact it's a great deal more efficient than your original approach.
Sure, using a parsing library is the industrial-strength solution, but we all have times were we just want to write something in 10 seconds and be done. Next time you want to process the meaty text of a page, ignoring tags, try just run your input through strip_tags first. This way you will get only the plain, visible text and your regex powers will again reign supreme.
This is so horrible I hesitate to post it, but if you want a quick hack, reverse the problem--instead of finding the stuff that isn't X, find the stuff that IS, change it, do the thing and change it back.
This is assuming you're trying to change awesome (to "wonderful"). If you're doing something else, adjust accordingly.
$string = 'Awesome is the man who <b>awesome</b> does and awesome is.';
$string = preg_replace('#(title\s*=\s*\"[^"]*?)awesome#is', "$1PIGDOG", $string);
$string = preg_replace('#awesome#is', 'wonderful', $string);
$string = preg_replace('#pigdog#is', 'awesome', $string);
Don't vote me down. I know it's hack.

Categories