note 101461 added to function.wordwrap

From: Date: Fri, 17 Dec 2010 17:01:46 +0000
Subject: note 101461 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-175032@lists.php.net to get a copy of this message
There is a contradiction with this code. Someone mentioned earlier that wordwrap cuts out "spaces," but to be specific, that means all white spaces: "\n" and "\r". So, let's say you want to wordwrap some text. It'll wrap it, but in doing so, deletes all or some of the "\n" and "\r." Instead, you decide to do an str_replace or preg_replace to change the "\n" and "\r"'s into <br> commands, right? That way, your returns and new lines are already coded in HTML when you do the wordwrap for the <wbr> tags (word-break). Oh, but wait, big problem -- now that you have <br> commands everywhere in the string, if you do a wordwrap of ANY html, you run the risk of it dropping RIGHT INTO the middle of another html command. Here's an example that I just produced: "Hey<wbr><br<wbr>><b<wbr>r>Y<wbr>eap," which should be "Hey\n\nYeap" properly parsed with <wbr> tags. I was hoping that maybe Firefox would just handle it, and compile the inside tag and then maybe the outside tag -- no way. The contradiction is this: no matter what you use wordwrap for in terms of producing data outputtable to a browser, you must first do an str_replace to change the linebreaks to <br> tags. And once you do that, you can't do a wordwrap, because it'll wreck your HTML code. Consider that you have user-input, and you already did an "htmlentities" that converts everything into unicoded characters. Then a wordwrap of some html code would totally wreck that. But, there's always a way, and someone earlier had an idea. I need to thank bruceboughton who posted below (way back in 2005) for his idea to use non-standard ASCII-chars. Anyhow, here's a perfect way to wordwrap user-inputted data: // $user_input is the original string we're printing // $user_input_to_print is the version we're printing // Double the spacing (in case wordwrap eats up a blank space) $needle = " "; $replacement = " "; $user_input_to_print = str_replace($needle, $replacement, $user_input); // Standard Stripslashes $website_quote_to_save_to_print = stripslashes($website_quote_to_save_to_print); // Change wordbreaks to chr(25) -- later to be replaced $needle = '/\n\r|\r\n/'; $replacement = chr(25); $website_quote_to_save_to_print = preg_replace($needle, $replacement, $website_quote_to_save_to_print); // Wordwrap with chr(26) -- later to be replaced $website_quote_to_save_to_print = wordwrap($website_quote_to_save_to_print, 3, chr(26), true); // Standard HTMLentities $website_quote_to_save_to_print = htmlentities($website_quote_to_save_to_print); // Change the chr(25) to a standard <br> tag $needle = chr(25); $replacement = "<br>"; $website_quote_to_save_to_print = str_replace($needle, $replacement, $website_quote_to_save_to_print); // Change the chr(26) to a standard <wbr> and &shy; tag // (&shy; is for IE compatibility) $needle = chr(26); $replacement = "<wbr>&shy;"; $website_quote_to_save_to_print = str_replace($needle, $replacement, $website_quote_to_save_to_print); // Fixing a weird placement that should have no effect, // But produces a weird rendering in Firefox. $needle = "<br><wbr>&shy;<br>"; $replacement = "<br><br>"; $website_quote_to_save_to_print = str_replace($needle, $replacement, $website_quote_to_save_to_print); // Your result. print("$website_quote_to_save_to_print"); Woooooorks beautifully. ---- Server IP: 69.147.83.197 Probable Submitter: 68.0.209.235 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/101461 Del: integrated -- https://master.php.net/note/delete/101461/integrated Del: useless -- https://master.php.net/note/delete/101461/useless Del: bad code -- https://master.php.net/note/delete/101461/bad+code Del: spam -- https://master.php.net/note/delete/101461/spam Del: non-english -- https://master.php.net/note/delete/101461/non-english Del: in docs -- https://master.php.net/note/delete/101461/in+docs Del: other reasons-- https://master.php.net/note/delete/101461 Reject -- https://master.php.net/note/reject/101461 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175032) next »