1

Hi I'm working on some formatting for mobile devices and according to my sources they work best with 37 characters per line.

Let's say I input a string into a form that is 'normal' say 80 characters per line such as :

INPUT:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce eget risus porta, sodales ligula sed, ultricies sem. Praesent cursus, erat et consequat cursus, ante nisl placerat neque, ut maximus massa ipsum quis turpis. In mollis viverra nisl vitae pellentesque. Ut et tristique nisi. Duis nec lacinia enim. Vestibulum ultricies risus sed nibh imperdiet, nec venenatis libero suscipit. Morbi accumsan purus lectus, et commodo risus blandit at. Morbi luctus lacus dapibus, tempus est et, sollicitudin est. Phasellus sodales sodales eros. Proin dapibus pulvinar diam, ut pretium nisl pulvinar a. Donec nec lectus urna. Curabitur eu dolor pharetra, facilisis metus vitae, consectetur orci. Curabitur quis egestas mi, non tincidunt nisl. 

I'd like the routine to put /n at around the 37 character mark so that it is easily readable on screen for mobile devices.

OUTPUT:

Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Fusce
eget risus porta, sodales ligula sed,
ultricies sem. Praesent cursus, erat
et consequat cursus, ante nisl
placerat neque, ut maximus massa
ipsum quis turpis. In mollis viverra
nisl vitae pellentesque. Ut et
tristique nisi. Duis nec lacinia
enim. Vestibulum ultricies risus sed
nibh imperdiet, nec venenatis libero
suscipit. Morbi accumsan purus
lectus, et commodo risus blandit at.
Morbi luctus lacus dapibus, tempus
est et, sollicitudin est. Phasellus
sodales sodales eros. Proin dapibus
pulvinar diam, ut pretium nisl
pulvinar a. Donec nec lectus urna.
Curabitur eu dolor pharetra,
facilisis metus vitae, consectetur
orci. Curabitur quis egestas mi, non
tincidunt nisl. 

Each line has a carriage return at 37 columns keeping words consistent.

This is part of an API we are writing and we need a /n written into the variable of the output string.

The input string is a form field i.e.

$input=$_POST['input'];

OUTPUT

<textarea name="str" id="textarea" cols="80" rows="20">
   [print $output code goes here]
</textarea>

A number of you are asking me why I'm wanting to do this. Here's the reason: specifically my instructions are to take a text input string and turn it into 18 point Tahoma with 37 characers with a <br> at the end of each line. I can do that part no problem, once I have a string with new lines or carriage returns, so I'm just breaking this post down into a simple string question rather than a debate in mobile email philosophy!

Hi when I input text that has punctuation like an apostrophe (') into a form and then feed that input back into a <textarea> it adds an extra \ to the output. I tried str_replace but it doesn't seem to remove it.

Input:

Additional Strategies To This Case Study – Apply These To Propel Search Triggers’ Power To The Fullest!

Keep an 'arsenal' of keyword phrases, which you then 'sprinkle' into your titles, tags, and text from time to time. That's really the key. Finding those initial words you're pursuing is the most important factor. And then being consistent with your content on the site over spans of time, strategically incorporating the words you generated from Search Triggers (ST).

OUTPUT

Additional Strategies To This Case
Study – Apply These To Propel
Search Triggers’ Power To The
Fullest!

Keep an \'arsenal\' of keyword
phrases, which you then \'sprinkle\'
into your titles, tags, and text from
time to time. That\'s really the key.
Finding those initial words you\'re
pursuing is the most important
factor. And then being consistent
with your content on the site over
spans of time, strategically
incorporating the words you generated
from Search Triggers (ST).

How can I remove the \ before the apostrophe's in the output string on a text area?

$str = str_replace( '', "\", $str);   

is invalid because the \ is treated as a literal. Thanks!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Viktor
  • 517
  • 5
  • 23
  • Is this coming from an api you are building and implementors want you to insert carriage returns to the output? Because I would think this to be a task more suited for presentation purposes (css/frontend) – georaldc Dec 07 '15 at 19:27
  • yes this is from an API I am building for email formatting for mobile devices. It would be great to also know how to do this same effect in CSS however that's not what I'm asking here. – Viktor Dec 07 '15 at 19:44
  • Which email app are you using that is so rubbish that it doesn't have wrapping? – rjdown Dec 07 '15 at 19:48
  • Every mobile device has an email application and it has been TESTED and proven that it is more readable if you send email that is 37 characters wide per line. It falls on us as programmers to take someone's long paragraphs and put hard line breaks. If you want to do the testing, go ahead. I'm just writing the code and its a simple string manipulation. I just don't know how to do it. If you look at 'formatit.com' that is what we need but we need it internal so that we can compose an email from an input string or database record. – Viktor Dec 07 '15 at 19:56
  • There is another article in stack overflow [link] http://stackoverflow.com/questions/5784185/php-library-for-creating-manipulating-fixed-width-text-files but its very complicated and it only deals with files and arrays and I don't believe it does the same thing. – Viktor Dec 07 '15 at 19:58

2 Answers2

1

The php function wordwrap might help you achieve what you want

http://php.net/manual/en/function.wordwrap.php

georaldc
  • 1,880
  • 16
  • 24
  • Thanks! thats what I was looking for. So how do i get my reputation up to 15 so I can vote 'up' on ya? – Viktor Dec 07 '15 at 20:09
0

The function wordwrap did the job. I didn't know such a function existed. Thank you!

<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
?>
Viktor
  • 517
  • 5
  • 23