Javascript string/variable concatenation without space - php

while using a ajax script, I made the php part echo a value then in the jQuery part I alert it.
So we have something like this:
<?php echo "1"; ?>
//Javascript
alert("the value is"+phpvalue);
The alert shows up like "the value is 1". Now my question is how do I remove the space between the "the value is" and +phpvalue? So it show up like "the value is1". I tried trim() on the php part but it doesn't change anything.
Apreciate any help and sorry if it is a noob question =/

In http response can be some other caracters than what you print. For example, if there is spaces before php tag .. BOM character in the begining of the php file... so if you trim just the variable, that does not detemrimate, that you get purely the variable on client side without junk characters.
So you should use trim on client side in javascript:
For example jQuery trim (http://api.jquery.com/jQuery.trim/)
alert("the value is"+$.trim(phpvalue));
Or use nativ js trim function from phpjsorg (http://phpjs.org/functions/trim/):
alert("the value is"+trim(phpvalue));

Not sure exactly what you want but here are two situations
If you just are dealing with excess whitespace on the beginning or end of the string you can use trim(), ltrim() orrtrim() to remove that.
If you are dealing with extra spaces within a string consider a preg_replace of multiple whitespaces " "* with a single whitespace " "
$foo = preg_replace( '/\s+/', ' ', $foo );

Related

Remove front and end white space from a string

I see this question has been asked a few times but I couldn't find an answer in php.
I have the following string.
$myString = "‍ LLC."
I run this trim($myString);
and I get this back "‍ LLC."
according to the trim documentation it should delete the white space in the front and the back? What am I missing?
I also tried htis trim($myString, " "); same results
The e2808d at the beginning of bin2hex() output is ZERO WIDTH JOINER character and the reason for trim() to not trim it. Try (PHP 7):
echo trim($myString, "\u{200d} \t\n\r\0\x0B");
I think this is happening because of the empty space is not really a real white space (tab) looks like a hidden character (â). i copy your variable and value code into an online PHP editor and i got this:

echo backspace inside string

Pretty simple but I'm looking for the easiest way (HEX?) and it's not working...
I want to add to the string backspaces (delete last character)...
Here is my simple code :
<?php
echo '<br>Delete me!!!'."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8";
?>
Small tweak needed here ;)
backspace is a special character that is interpreted by editors and various text input boxes as a request to remove the character before the cursor. The backspace character itself has no magical powers, it cannot make disappear something that was already displayed.
If you need to remove some characters from a string in PHP you can use substring(), str_replace(), preg_replace() or other string handling function.
For example, you can ignore the last 3 characters from a string like this:
echo(substr('blah-blah', 0, -3));

Smart trim() option in php and jQuery

This is the problem that I have in PHP and jQuery.
I want to make a "smart" function that will not only clean white space, but also a certain character at the beginning and end of the string. Why?
I made administration panel who manage with subdomains. Allowed characters is [a-z0-9-.]. If someone accidentally write "-my.domanin." or ".my-domain" or "-my-domain-" or a similar variation is problem. I need to trim that to look like this "my-domain" or "my.domain"
Is this possible? Thanks!
function mytrim($stringToTrim) {
return trim($tringToTrim, " \t\n\r\0\x0B.-");
}
The first part of the string, " \t\n\r\0\x0B", strips white space from the beginning and ends and the second part, ".-" strips the "." and "-" characters.
Clearly you could just use trim() in your script, I just put it in a function for the example.

%0D%0A not creating a newline when echo is called

Every time I try to echo a string there is no new line. I how can I make a newline when calling echo in php using the $_GET?
here is my code:
<?php
$text = "Hello world";
$text2 = $_GET['msg'];
echo $text2
?>
and this is what I enter in the url:
http://localhost/hello.php?msg=hello%0Dworld
or this one:
http://localhost/hello.php?msg=hello%0Aworld
and even this one:
http://localhost/hello.php?msg=hello%0D%0Aworld
The echo has to be a newline please don't say I should use a different method than $_GET. It has to be $_GET
While performing your exercises you are creating an HTML page.
HTML is a special markup language, which renders according to set of rules, some of them are:
<> characters has a special meaning of control structures named tags
all newline characters are ignored
to make a newline on the page, one have to use suitable tag - such as <br>, <p> or whatever.
So, to make a newline appear on your page, you have to convert newline characters to tags. Either use nl2br() function to get a <br /> tag or str_replace() if you want any other one
Be aware that echoing any request variables without validating them is a considerable security risk! If you want to publish any application with this code it needs to be redesigned.
As common sense states, the conversion from urlencoded to the corresponding character is automatically done by php, but HTML does not render such characters, so you either need to convert them into linebreaks or enclose the message in <pre> tags.

PHP trimming spaces unnecessarily?

Let's say I have a variable $foo:
$foo = " a a blablabla a a";
But when I do var_dump($foo) the following gets outputted:
string(57) " a a blablabla a a"
It's like the length (57 in this case) it's correct and it counts the spaces, but it doesn't display them.
How can I display the full string, including the multiple spaces in between the other characters?
If you use <pre> tag the text appears in the brower as you typed it. Couple of links:
http://www.tizag.com/htmlT/htmlpre.php
http://www.htmlcodetutorial.com/linepar/_PRE.html
You could also replace spaces with non breaking space .
Web browsers ignore a lot white space in the code which is nice. Otherwise we wouldn't be able to intend our source code or use newlines much...
Are you by any chance doing this in a browser. If so you need to do this.
<pre>
<?php echo var_dump($foo); ?>
</pre>
Because I just tried in command line and I get the output with spaces. Browsers don't handle multiple spaces and they trim them down. If you want a browser to handle multiple spaces you have to use the output inside a <pre> tag or use instead of spaces.
something like echo str_replace(' ', ' ',$test);

Categories