wordwrap with a twist?

This is a discussion on wordwrap with a twist? within the PHP Language forums, part of the PHP Programming Forums category; I need to wrap text with a negative indent, that is this: Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-24-2005
Pierre Jelenc
 
Posts: n/a
Default wordwrap with a twist?


I need to wrap text with a negative indent, that is this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

gives this:


Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo
consequat.
Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id
est laborum.

Is there a clever way to do this? All I can come up with is to parse
painfully the strings word-by-word and count the characters, which on a
long list (hundreds, possiblyly thousands of lines) is quite resource-
intensive.

(the indent can be tabs or spaces, it does not matter)

Thanks,

Pierre
--
Pierre Jelenc | New on Home Office Records: Ethan Lipton
| www.homeofficerecords.com www.ethanlipton.com
The Gigometer | Pepper Of The Earth: the HO blog
www.gigometer.com | www.homeofficerecords.com/blog
Reply With Quote
  #2 (permalink)  
Old 01-24-2005
Disco Octopus
 
Posts: n/a
Default Re: wordwrap with a twist?

Pierre Jelenc wrote :
> I need to wrap text with a negative indent, that is this:
> gives this:
>
>
> Lorem ipsum dolor sit amet, consectetur adipisicing
> elit, sed do eiusmod tempor incididunt ut
> labore et dolore magna aliqua.
> Ut enim ad minim veniam, quis nostrud exercitation
> ullamco laboris nisi ut aliquip ex ea commodo
> consequat.


try this....
http://au.php.net/manual/en/function.wordwrap.php


then do a search/replace for each "\n" replacing it with "\t\n".

havent tried it, but should be fairly simple.... i think.

--
dance with children in the rain

Reply With Quote
  #3 (permalink)  
Old 01-24-2005
Disco Octopus
 
Posts: n/a
Default Re: wordwrap with a twist?

Disco Octopus wrote :
> Pierre Jelenc wrote :
>> I need to wrap text with a negative indent, that is this:
>> gives this:
>>
>>
>> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
>> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
>> veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
>> commodo consequat.

>
> try this....
> http://au.php.net/manual/en/function.wordwrap.php
>
>
> then do a search/replace for each "\n" replacing it with "\t\n".


..... or rather.....

do a search/replace for each "\n" replacing it with "\n\t".

--
dont pick your nose if it is sore

Reply With Quote
  #4 (permalink)  
Old 01-24-2005
Disco Octopus
 
Posts: n/a
Default Re: wordwrap with a twist?

Pierre Jelenc wrote :
> I need to wrap text with a negative indent, that is this:
>


> gives this:
>
>
> Lorem ipsum dolor sit amet, consectetur adipisicing
> elit, sed do eiusmod tempor incididunt ut
> labore et dolore magna aliqua.


OK. i tried this....

<?php

$v_1 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
$v_1 = wordwrap ( $v_1, 50, "\n", 1);
echo "<pre>\n";

// do some kinda loop here to get your parapraphs/sentences out of each
string.

$v_1 = str_replace ( "\n", "\n\t", $v_1);
echo $v_1;

// end some kinda loop


echo "</pre>\n";
?>

--
if the oil light is on, dont think it will just go away

Reply With Quote
  #5 (permalink)  
Old 01-24-2005
Pierre Jelenc
 
Posts: n/a
Default Re: wordwrap with a twist?

Disco Octopus <discooctopus@yahoo.com> writes:
> Pierre Jelenc wrote :
> > I need to wrap text with a negative indent, that is this:


*negative indent* is the crucial phrase.

> $v_1 = str_replace ( "\n", "\n\t", $v_1);
> echo $v_1;


All this does is shift the first wrapped line to the left, however it is
still only the same size as the others' text, while it should be longer:

XXXXXXXXXXXXXXXXXXXXX
YYYYYYYYYYYYYYYYY
ZZZZZZZZZZZZZZZZZ

The solution turns out to chop off the start of the text, wordwrap and add
the tabs, then add back the chopped piece at the start of line one.

Pierre
--
Pierre Jelenc | New on Home Office Records: Ethan Lipton
| www.homeofficerecords.com www.ethanlipton.com
The Gigometer | Pepper Of The Earth: the HO blog
www.gigometer.com | www.homeofficerecords.com/blog
Reply With Quote
  #6 (permalink)  
Old 01-25-2005
Disco Octopus
 
Posts: n/a
Default Re: wordwrap with a twist?

Pierre Jelenc wrote :
> Disco Octopus <discooctopus@yahoo.com> writes:
>> Pierre Jelenc wrote :
>>> I need to wrap text with a negative indent, that is this:

>
> *negative indent* is the crucial phrase.
>
>> $v_1 = str_replace ( "\n", "\n\t", $v_1);
>> echo $v_1;

>
> All this does is shift the first wrapped line to the left, however it is
> still only the same size as the others' text, while it should be longer:
>
> XXXXXXXXXXXXXXXXXXXXX
> YYYYYYYYYYYYYYYYY
> ZZZZZZZZZZZZZZZZZ
>
> The solution turns out to chop off the start of the text, wordwrap and add
> the tabs, then add back the chopped piece at the start of line one.
>
> Pierre


Not sure what you mean by *negative indent*

.... but I did want to give it another go anyway. this is what I came up
with....

<?php

$v_indent = 4; // the size of the indent in characters
$v_wrapat = 50; // the wrap line at size
$v_nl = "\n";

$v_full_story = "Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.";

$v_paragraphs = explode ( $v_nl, $v_full_story);
echo "<pre>" . $v_nl;

reset($v_paragraphs);
while (list($key, $v_paragraph) = each($v_paragraphs)) {

$v_firstline = substr ( $v_paragraph, 0, $v_indent + $v_wrapat);

$v_restlines = substr ( $v_paragraph, $v_indent + $v_wrapat);

$v_restlines = wordwrap ( $v_restlines, 50, $v_nl, 1);
echo $v_firstline . $v_nl;
$v_restlines = str_replace ( $v_nl, $v_nl . " ",
$v_restlines);
echo " " . $v_restlines;
}

echo $v_nl . "</pre>" . $v_nl;
?>

--
if the oil light is on, dont think it will just go away

Reply With Quote
  #7 (permalink)  
Old 01-25-2005
Disco Octopus
 
Posts: n/a
Default Re: wordwrap with a twist?

Disco Octopus wrote :
> Pierre Jelenc wrote :
>> Disco Octopus <discooctopus@yahoo.com> writes:
>>> Pierre Jelenc wrote :
>>>> I need to wrap text with a negative indent, that is this:

>>
>> *negative indent* is the crucial phrase.



and then i thought about it, and got this...

*Attempt 234* (and counting)

i think this will do the trick.


<?php

$v_indent = 3;
$v_wrapat = 40;
$v_nl = "\n";
$v_dent = "";

$v_i = 0;
while ($v_i < $v_indent) {
$v_dent = $v_dent . " ";
$v_i++;
}

$v_full_story = "This will wrap the first line of each paragraph at
character $v_wrapat or earlier. Each subsequent line will be wrapped
at character $v_wrapat less $v_indent. The \"less $v_indent\" takes
into account the extra characters placed before each of the lines for
the actual indent.
This will wrap the first line of each paragraph at character $v_wrapat
or earlier. Each subsequent line will be wrapped at character
$v_wrapat less $v_indent. The \"less $v_indent\" takes into account
the extra characters placed before each of the lines for the actual
indent.";

$v_paragraphs = explode ( $v_nl, $v_full_story);
echo "<pre>" . $v_nl;

reset($v_paragraphs);
while (list($key, $v_paragraph) = each($v_paragraphs)) {

$v_firstline = wordwrap ( $v_paragraph, $v_wrapat, $v_nl, 1);
$v_temp = explode ( $v_nl, $v_firstline);
$v_firstline = $v_temp[0];

$v_len = strlen ($v_firstline);
$v_restlines = substr ( $v_paragraph, $v_len + 1);

$v_restlines = wordwrap ( $v_restlines, $v_wrapat - $v_indent -
1, $v_nl, 1);
echo $v_firstline . $v_nl;
$v_restlines = str_replace ( $v_nl, $v_nl . $v_dent,
$v_restlines);
echo $v_dent . $v_restlines;
}

echo $v_nl . "</pre>" . $v_nl;
?>

--
if you pay for your gym memebership, use it

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:18 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0