Remove front and end white space from a string - php

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:

Related

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.

PHP Detect 1 or more unicode spaces only

I have searched in vain to find a fix for this issue. I have an editable field in a web page that contains a user entered space. When I copy the space and enter it into a program called IVI32 which I guess you would call a Unicode text program, I get the following info.
The space character is defined as FFFE2000. I need to detect when this field has one or more of these spaces and nothing else. I have tried the following with preg_match:
'/\s+/u'
'/^[0 :-]+$/ '
'/\A\s*\z/'
Nothing works and I am completely stumped. Any help from some Unicode experts out there will be greatly appreciated.
There was an error in my code which was preventing anything from working properly (the product of no time off!). Here's what works for anyone else who might want to detect if an element contains only whitespace that cannot be eliminated by php trim();
if(!preg_match('/\\s/', $test_string)):-do something-
if(!preg_match('/\s+/u', $test_string)):-do something-
if(!preg_match('/[\pZ\pC]+/u', $test_string)):-do something-
For anyone who is interested the space is pasted immediately after the end of this sentence.
Would this work?
preg_match('/^ +$/', $subject);
Match a single or more spaces? Because \s will also match nonbreaking spaces, tabs, and newlines.
Have a try with:
/\p{WhiteSpace}/

why does str_replace replaces my character with a semicolon?

I'm new to PHP (so no bullying please) and I'm trying to trim a variable to remove quotation marks. For some reason, my quotation marks are replaced by semicolons, anybody knows why ?
Where $movieArray["title"] = '"Veronica Mars"';
str_replace('&#x22', '', $movieArray["title"]);
output: ;Veronica Mars;
Also, the reason I have this &#x22 instead of this " is that the trim doesn't work with ".
Thanks
Your text is not "Veronica Mars". It's probably this:
"Veronica Mars"
If you strip &#x22, only the ; remains.
What you see in the browser screen is the result of rendering some HTML code.

Javascript string/variable concatenation without space

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 );

Trying to remove "»" with php

I'm trying to remove this character, and this character only from a $string with php. I've tried str_replace("»","",$test) and str_replace(chr(187),"",$test) but they can't touch it. The issue is it isn't in the same spot every time, so I can't even get creative with trimming the ends.
Are you forgetting that str_replace(old, new, string) doesn't modify the original string, but rather returns a copy of the modified string?
So:
$string = "This is the » character";
$new_string = str_replace("»", "_", $string);
echo $new_string;
Should work (it does for me)!
Wanted to point out that "»" in HTML equals » which is a standard. So, my advise would be that you better use standard characters.
Reference of characters: http://www.w3schools.com/tags/ref_entities.asp
I ended up stepping through the string one character at a time with
echo ord($test[n]
until I finally found the offending hidden character that was ruining my night.
Thanks for all the help everyone!

Categories