Remove white space from first line of textarea - php

how can i prevent php to remove the whitespace from the begining of the textarea first line. Everytime i submited the form, the withespace are removed...even if i replace it for nbsp;
The code i'm using:
PHP
if(isset($_POST['btn_cel'])){
$text = trim($_POST['cel']);
$text = explode("\n", $text);
foreach($texto as $line){
echo str_replace(' ',' ',$line);
}
}
The input
1234 5678
abcdefghif
The output
1234 5678
abcdefghif

Get rid of the call to trim.
This function returns a string with whitespace stripped from the beginning and end of str.

From the PHP documentation :
trim — Strip whitespace (or other characters) from the beginning and end of a string

You should not be using the trim() function if you want php to leave whitespaces as it is. If you apply trim on some string it removes whitespaces from the beginning and end of it.

if(isset($_POST['btn_cel'])){
$text = trim($_POST['cel']);
$text = explode("\n", $text);
foreach($texto as $line){
echo trim(str_replace(' ',' ',$line));
}
}

Related

PHP Remove extra spaces between break lines

I have a string in PHP, i'm able to remove multiple continuous break lines and multiple spaces, but what i'm still not able is to remove multiple break lines if i have an space in the middle.
For example:
Text \r\n \r\n extra text
I would like to clean this text as:
Text \r\nextra text
Could also be too:
Text \r\n \r\nextra text
Don't need to be an extra espace after the break line.
What i have right now is:
function clearHTML($text){
$text = strip_tags($text);
$text = str_replace(" ", " ", $text);
$text = preg_replace("/[[:blank:]]+/"," ",$text);
$text = preg_replace("/([\r\n]{4,}|[\n]{2,}|[\r]{2,})/", "\r\n", $text);
$text = trim($text);
return $text;
}
Any suggestions?
To remove extra whitespace between lines, you can use
preg_replace('~\h*(\R)\s*~', '$1', $text)
The regex matches:
\h* - 0 or more horizontal whitespaces
(\R) - Group 1: any line ending sequence (the replacement is $1, just this group vaue)
\s* - one or more whitespaces
The whitespace shrinking part can be merged to a single preg_replace call with the (?: |\h)+ regex that matches one or more occurrences of an string or a horizontal whitespace.
NOTE: If you have Unicode texts, you will need u flag.
The whole cleaning function can look like
function clearHTML($text){
$text = strip_tags($text);
$text = preg_replace("~(?: |\h)+~u", " ", $text);
$text = preg_replace('~\h*(\R)\s*~u', '$1', $text);
return trim($text);
}

Break Long Strings Of Text That Don't Contain A Space

How can I put spaces to a long string that does not have spaces
Example : 5Bedroom.Apartment,in.NewYork>City
I want to put spaces after any dot and comma. Only if no space after dot and comma. If already have space, just ignore
you should replace the charector which u want
$str = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $str);
Such regex ~(?<=[,.])(?=\S)~ matches position after comma or dot before not space
$str = preg_replace( ~(?<=[,.])(?=\S)~, " ", $str);
demo

jQuery TRIM space between 2 alphabets or words

I want to remove white spaces at the beginning and end of the words. Also there should be just 1 character space between 2 words. I use below code but it only remove white spaces in the beginning of the first word and at the end. How can I remove extra space between 2 words.
jQuery Trim
var fullname= jQuery('#fullname').val();
fullname= jQuery.trim(fullname);
if(fullname.length == 0) {
var error = true;
jQuery('#fullname_error').fadeIn(500);
} else {
jQuery('#fullname_error').fadeOut(500);
}
PHP Trim
$fullname = mysql_real_escape_string(trim($_POST["fullname"]));
you can use preg_replace()
preg_replace('/ {2,}/', ' ', $string);
this code will found any sequence of spaces longer than 2 and replace it by one space
upd: also you can use JavaScript native function replace:
string.replace(/ {2,}/, ' ');
is there a way to trim extra space between 2 words?
Yes.
var str = "foo bar etc";
str = $str.replace(/\b\s{2,}\b/g, ' ');
Or in PHP
$str = preg_replace('/\b\s{2,}\b/', ' ', $str);
Here are String prototypes for trimming.
String trim prototypes
Include the following definition in your code.
String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};
Then use 'fulltrim' instead of 'trim' in your code. 'fulltrim' will remove spaces from the front, end, and the middle all at once.
Here is an example Plunker

How do you strip whitespace from user submitted data that is not an array PHP?

I was wondering how can I strip white space from elements that are just whitespace and whitespace from all elements from user submitted data using PHP?
lets say if a tag is stripped how can
I stop that from entering the
database?
$sRaw = $_POST[ 'data' ];
$sTrimmed = trim( $sRaw );
if( $sRaw === $sTrimmed ) {
// DB insert code
} else {
// Message was trimmed, show user an error
}
Very simple.
$string = " Whats up I'm cool?";
$string = trim($string);
$string = str_replace(" ", " ", $string);
$string = str_replace(" ", " ", $string);
echo $string; //output is "Whats up I'm cool?"
The reason is for this is because trim() removes any whitespace which is deemed useless thus reducing the total size of the string. The only thing is trim() only removes the whitespace at the beginning and end, so I've added two str_replace() which have been set to remove unwanted whitespace, and because if there's " " (three spaces) one str_replace() won't cut it so I've added it twice, and if you want to, you can add a cycle using foreach() which will trim it until there's no whitespace left but I have wrote it in the basic form as that's what you're asking for.
Depends on the white space... but I believe you are asking about trim() which removes starting and ending whitespace.
echo trim(" v "); //results in "v"

How to trim each line in a heredoc (long string) in PHP

I'm looking to create a PHP function that can trim each line in a long string.
For example,
<?php
$txt = <<< HD
This is text.
This is text.
This is text.
HD;
echo trimHereDoc($txt);
Output:
This is text.
This is text.
This is text.
Yes, I know about the trim() function, but I am just not sure how to use it on a long strings such as heredoc.
function trimHereDoc($t)
{
return implode("\n", array_map('trim', explode("\n", $t)));
}
function trimHereDoc($txt)
{
return preg_replace('/^\s+|\s+$/m', '', $txt);
}
^\s+ matches whitespace at the start of a line and \s+$ matches whitespace at the end of a line. The m flag says to do multi-line replacement so ^ and $ will match on any line of a multi-line string.
Simple solution
<?php
$txtArray = explode("\n", $txt);
$txtArray = array_map('trim', $txtArray);
$txt = implode("\n", $txtArray);
function trimHereDoc($txt)
{
return preg_replace('/^\h+|\h+$/m', '', $txt);
}
While \s+ removes empty lines, keeps \h+ each empty lines

Categories