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}/
Related
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:
So I've got this piece of code that wont play nice.
preg_match_all("/(\{\[)([\w-\d\s\.\|']*)(\]\})/i",$replace_text, $match);
What it is supposed to do, is allow an apostrophe to be in my replacement text. So in my text, where i have "{[SPIN--they are|they’re]}" it should return "they are" or "they're".
But instead, it simply does nothing and spits out the entire spintax code just as I typed above.
The only time this does not work, is when a replacement text has an apostrophe. It works perfectly everywhere else. Been trying to fix this for two days and I'm about to throw my keyboard through my monitor.
There are many things that my project does and it is imperative to have the {[SPIN-- before specifying the replacement text, and the ]} closing brackets.
Can someone help, please?
In your example string it's not a single quote character, but something that looks similarly
’ (the actual character) vs ' (that's what you think it is)
I've asked this question before but I didn't seem to get the right answer. I've got a problem with new lines in text. Javascript and jQuery don't like things like this:
alert('text
text);
When I pull information from a database table that has a break line in it, JS and jQuery can't parse it correctly. I've been told to use n2lbr(), but that doesn't work when someone uses 'shift+enter' or 'enter' when typing text into a message (which is where I get this problem). I still end up with separate lines when using it. It seems to correctly apply the BR tag after the line break, but it still leaves the break there.
Can anyone provide some help here? I get the message data with jQuery and send it off to PHP file to storage, so I'd like to fix the problem there.
This wouldn't be a problem normally, but I want to pull all of a users messages when they first load up their inbox and then display it to them via jQuery when they select a certain message.
You could use a regexp to replace newlines with spaces:
alert('<?php preg_replace("/[\n\r\f]+/m","<br />", $text); ?>');
The m modifier will match across newlines, which in this case I think is important.
edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.
edit2: like #LainIwakura, I made a mistake in my regexp, partly due to the previous edit. my new regexp only replaces CR/NL/LF characters, not any whitespace character (\s). note there are a bunch of unicode linebreak characters that i haven't acknowledged... if you need to deal with these, you might want to read up on the regexp syntax for unicode
Edit: Okay after much tripping over myself I believe you want this:
$str = preg_replace('/\n+/', '<br />', $str);
And with that I'm going to bed...too late to be answering questions.
I usually use json_encode() to format string for use in JavaScript, as it does everything that's necessary for making JS-valid value.
I've checked google for help on this subject but all the answers keep overlooking a fatal flaw in the replacement method.
Essentially I have a set of emoticons such as :) LocK :eek and so on and need to replace them with image tags. The problem I'm having is identifying that a particular emoticon is not part of a word and is alone on a line. For example on our site we allow 'quick links' which are not included in the smiley replacement which take the format go:forum, user:Username and so on. Pretty much all answers I've read don't allow for this possiblity and as such break these links (i.e. go<img src="image.gif" />orum). I've tried experimenting around with different ways to get around this to check for the start of the line, spaces/newline characters and so on but I've not had much luck.
Any help with this problem would be greatly appreciated. Oh also I'm using PHP 5 and the preg_% functions.
Thanks,
Rupert S.
Edit 18/04/2011:
Thanks for your help peeps :) Have created the final regex that I though I'd share with everyone, had a couple problems to do with special space chars including newline but it's now working like a dream the final regex is:
(?<=\s|\A|\n|\r|\t|\v|\<br \/\>|\<br\>)(:S)(?=\s|\Z|$|\n|\r|\t|\v|\<br \/\>|\<br\>)
To complete the comment into an answer: The simplest workaround would be to assert that the emoticons are always surrounded by whitespace.
(?<=\s|^)[<:-}]+(?=\s|$)
The \s covers normal spaces and line breaks. Just to be safe ^ and $ cover occurrences at the start or very end of the text subject. The assertions themselves do not match, so can be ignored in the replacement string/callback.
If you want to do all the replace in one single preg_replace, try this:
preg_replace('/(?<=^|\s)(:\)|:eek)(?=$|\s)/e'
,"'$1'==':)'?'<img src=\"smile.gif\"/>':('$1'==':eek'?'<img src=\"eek.gif\"/>':'$1')"
,$input);
I have a text area in HTML where the user can enter text, but when the form is submitted, and its passed to a php script which echo's it, there aren't any newlines. Knowing that HTML does that, I tried doing a preg_replace() before echoing it...
echo preg_replace("/\n/", "<br />", $_GET["text"]);
but still everything is on one line.
So my best guess is that HTML textareas use a different newline character... can anybody shed some light on the subject?
EDIT
Ok, so I've figured out the problem: The Javascript is stripping the newlines. view code here
EDIT 2
Ok, so thanks to Jason for solving this problem. I needed to do:
escape(document.getElementById('text'));
Instead of just:
document.getElementById('text');
and the newlines are preserved, problem solved!
echo nl2br($_GET['text'])
Though, your preg_replace worked for me!
usually when testing for newlines in any string, I use /[\n\r]/, just to cover my bases. My guess is that this would match the new lines.