textbox formatting challenge

8 posts by 3 authors in: Forums > CMS Builder
Last Post: May 17, 2010   (RSS)

By Deborah - May 13, 2010 - edited: May 13, 2010

I have a textbox with which I am using 'htmlspecialchars'. I would like to retain the line breaks generated by user input, but take care of any other characters.

What I see in the page source code is:
<br/>

Which of course results in the line break not happening and the br tag appearing on the displayed page like this:
first line<br/>second line<br/>third line

Does anyone have any custom PHP that will override the htmlspecialchars for just the br tag?

Deborah

Re: [Deborah] textbox formatting challenge

By Chris - May 13, 2010

Hi Deborah,

I typically solve this with [url http://php.net/manual/en/function.nl2br.php]nl2br()[/url], which converts newlines into <br>s. If you're starting out with newlines, you would do nl2br(htmlspecialchars($record['content'])), which would first escape HTML, then replace newlines with <br>s.

The above assumes you're working with newlines. If instead you're working with <br>s, you could first replace the <br>s with newlines, then use the same approach:

$content = 'hello<br><a href="http://google.com/">world';
$content = preg_replace('#<br\s*/?>#i', "\n", $content);
echo nl2br(htmlspecialchars($content));


That preg_replace pattern should catch variations on <br>, such as <br/>, <br />, <BR/>, etc.

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] textbox formatting challenge

By Deborah - May 14, 2010

Chris, That works for me if I remove the line break. Using the line break causes an extra br tag.

<p><?php $record['address_phone'] = preg_replace("#<br\s*/?>#i", "", $record['address_phone']);
echo nl2br(htmlspecialchars ($record['address_phone'])) ?></p>


Now if only I could combine that with a means of stripping out extra line breaks from user input.

Here's what that looks like now:
<p>line one<br />
line two<br />
<br />
line three<br />
<br />
<br />
</p>


I want it to look like:
<p>line one<br />
line two<br />
line three<br />
</p>


Do you know if it is possible to accomplish both?

Thanks for the assistance so far!
Deborah

Re: [Deborah] textbox formatting challenge

By Jason - May 14, 2010

Hi,

Try using this code:

<?php $record['address_phone']=preg_replace('`[\r\n]+`',"\n",$record['address_phone']); ?>
<p><?php $record['address_phone'] = preg_replace("#<br\s*/?>#i", "", $record['address_phone']);
echo nl2br(htmlspecialchars ($record['address_phone'])) ?></p>


That should remove the extra line breaks.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] textbox formatting challenge

By Deborah - May 14, 2010 - edited: May 14, 2010

Jason,

First of all, I so appreciate your help!

Maybe my setup is different than what you are assuming I have in my template, but using the code provided, I am still seeing the extra line breaks.

I've attached a stripped down version of the page template that might be easier to decipher than my forum code postings.

I realize this is not at all a CMSB issue, but rather a PHP solution I'm trying to work out for the benefit of most of my projects. (I've find that most of my clients are not aware of their extra line breaks and can leave as many as five after an entry! Yikes!)

I am greatly appreciative of any suggested solution for this.

Thanks again.
Deborah
Attachments:

test_009.php 2K

Re: [Deborah] textbox formatting challenge

By Jason - May 17, 2010

Hi Deborah,

Try using this code:

<?php foreach ($lodging_listingsMHRecords as $record): ?>
<h3><?php echo htmlspecialchars ($record['business_name']) ?></h3>
<?php $record['address_phone']= preg_replace('`[\r]+`',"\n",$record['address_phone']); ?>
<?php $record['address_phone']= preg_replace('`[\r\n]+`',"\n",$record['address_phone']); ?>
<p><?php $record['address_phone'] = preg_replace("#<br\s*/?>#i", "", $record['address_phone']);
echo nl2br(htmlspecialchars ($record['address_phone'])) ?></p>
<?php endforeach ?>


Give this a try.

If this doesn't work, when you view the page source, is there just extra breaks, or are you seeing extra <br /> tags?

If you can't get it working, maybe you could send me CMS login and FTP details and I could take a look. Just email them to jason@interactivetools.com. Don't post this information to the forum.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] textbox formatting challenge

By Deborah - May 17, 2010 - edited: May 17, 2010

Jason,

The new code worked!

The code will leave one stray <br /> before the closing</p>, if a line break was entered after the last word typed, but this does not cause a display issue on the web page.

That really takes care of the giant white spaces I see from user entry.

Thanks so much for your help!
Deborah


*EDITED by Deborah:
I added the following line, which took out the <br /> at the end of the string:
$address_phone = rtrim($address_phone, '<br />');

Final code:

<?php foreach ($lodging_listingsMHRecords as $record): ?>
<?php $address_phone=nl2br($record['address_phone']); ?>
<p><?php $address_phone = preg_replace("/(<br\s*\/?>\s*)+/", " <br />", $address_phone);
$address_phone = rtrim($address_phone, '<br />'); ?>
<?php echo $address_phone; ?></p>
<?php endforeach ?>