Jump to content


Photo

wordwrap() html


  • Please log in to reply
3 replies to this topic

#1 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 28 July 2006 - 05:04 PM

How do you wordwrap html? I have a crappy little mailing list thing I made that sends an html email. Whenever I try to wordwrap() $message with "<.br />\n", the email won't send (and I'm assuming that's becuase html tags are getting broken up with "<.br />\n", which is bad). Any suggestions?

And when's phpfreaks gonna fix the <.br /> problem?
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 10:27 PM

You shouldn't need to wordwrap those, just take a few extra seconds to add in br tags.  Be careful with html emails it ALWAYS double emails.  one with html and one without I believe is what the manual says

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 July 2006 - 06:26 PM

This should work

<?php
function htmlwordwrap ($txt, $n, $break="\n", $split=0) {
	$res = '';
	$pos = $k = $intag = 0;
	$L = strlen($txt);
	while ($pos < $L) {
                $ch = $txt{$pos};
		$res .= $ch;
		switch ($ch) {
			case '<':	$intag = 1;
						break;
			case '>':	$intag = 0;	    
						break;
			case ' ':	if (!$intag) {
							$x = $split ? 0 : 5;
							if ($n < $k+$x)	{
								$res .= $break;
								$k = 0;
							}
							else $k++;
					  }
					  break;
			default:	if ($split) {
							if (!$intag) {
								if ($n < $k)	{
									$res .= $break;
									$k = 0;
								}
								else $k++;
							}
						}
						else {
							if (!$intag) $k++;
						}
		}
		$pos++;
	}
	return $res;
}

echo htmlwordwrap($html,40, "<br/>\n", 0);

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 30 July 2006 - 03:55 AM

You shouldn't need to wordwrap those, just take a few extra seconds to add in br tags.  Be careful with html emails it ALWAYS double emails.  one with html and one without I believe is what the manual says

Where does it say that?
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users