/    Sign up×
Community /Pin to ProfileBookmark

help reformatting form “textarea” data in PHP.

I have a problem with multiple email forms in various sites. They are all submitted and processed by the same PHP module, so it makes sense to correct it there. Due to a bug in some older browsers I still support, I had to make all my mailform “textarea” fields use the “wrap=’phsyical” (same as wrap=hard) attribute. Otherwise the visitors entered text would cause the textarea to scroll horizontally at the right edge instead of “soft’ wrapping as they type. (In other words, wrap=”soft” or wrap=”virtual” wasn’t always working right in all browsers). Maybe this is no longer a problem in modern browsers, but for now I just want to correct the problem in the common PHP module.

Its easy enough to scan the PHP variable holding the textarea data, replacing all occurrences of the CR/LF combination with a space character. But when a visitor is using the form they are likely to format their message themselves, putting two line breaks where they want a paragraph break. So my routine will have to make sure that occurrences of two CR/LF combinations are left alone.

OK, this is not rocket science, and I guess I could come up with a routine to do it. But it seems like such a common problem, I though maybe it was a wheel that didn’t need to be reinvented, or maybe there is already a library function to do exactly what I’m looking for. So any suggestions are appreciated. I have an idea, but I’m not really good at regex, so don’t laugh…

“`
function removeSingleLinefeeds($string) {
return preg_replace(‘~R{1,}~’, ”, $string);
}
“`

Thanks!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 01.2021 — Not claiming this is the best way to do it, more that I was curious if it would work -- which it seems to do?
[code=php]
$regexes = array('/(r?n){2,}/' => '<{[P]}>', '/[rn]+/' => ' ', '/<{[P]}>/' => "nn");
return preg_replace(array_keys($regexes), $regexes, $string);
[/code]

It seems that it processes the first regex all the way through, then the 2nd, and so forth, so it's basically equivalent in this case of doing 3 separate, sequential "normal" calls to that function, but now crammed all into one command so that it at least _looks_ more efficient. ;)
Copy linkTweet thisAlerts:
@PeterPan_321authorMar 01.2021 — Thanks @NogDog#1628750 . I soon realized my single line solution was not going to work, and required some kind of multiple step process. So I tried the below instead. As you can see, I also was leaning toward 3 separate steps, and figured I'd better separate them for easy troubleshooting. It worked, but I kind of think my solution here is a dumb Kludge!

``<i>
</i>function removeSingleLinefeeds($string) {
$tmp = "ABCZXY"; // an unlikely string;
$data = $string;
$data = preg_replace('~R{2,}~', $tmp, $data); // swap multi linefeeds with tmp
$data = preg_replace('~R~', ' ', $data); // swap single linefeeds with space
$data = str_replace ( $tmp , "rnrn", $data); // swap multi LFs with doubles
return $data;
}<i>
</i>
``


Like I said, it works, unless somebody sends me a message with a bunch of intentional instances of "ABCZXY" (LOL!). I've not tried using PHPs replace functions with arrays yet as you've done, but I'll give yours a try too. Right now I just realized I have another dumb issue, forcing all my PHP errors to go to that same directory, so I have to table this to figure that out. But I'll let you know if your solution works too. Thanks again!
×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...