marcusfaye87 Posted June 9, 2008 Share Posted June 9, 2008 I am trying to match a string that contains 41 or more characters in a row without a newline or space in it. '/[^\n\s]{41,}/i' that's what I have got for the regex, but it's not working :s it's still counting newlines and spaces. any Idea's? EDIT: I'm trying to do this because I don't want to allow things like: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa more than 40 characters without a space or linebreak messes up my layout. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 9, 2008 Share Posted June 9, 2008 There's no need to use \n with \s. In fact, you can just use \S{41,}. In either case, the pattern matches your string. Also see wordwrap. Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 Yes, but I don't want it to always cap at that, just if a part of the string is longer than 40 characters to not allow it. I'm also allowing youtube codes and such, since they are longer than 40 characters, they need to be allowed <?php /** Handle tags */ if ($post['type'] == 1) { $tempString = preg_replace ('/\[youtube\](.*)\[\/youtube\]/i', ' ', $post['body']); $tempString = preg_replace ('/\[img\](.*)\[\/img\]/i', ' ', $tempString); } else { $tempString = $post['body']; } $tempString = str_replace ('\r', '', $tempString); if (preg_match ('/[^\s]{41,}/i', $tempString)) { $errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks'; } ?> that's my code for it Quote Link to comment Share on other sites More sharing options...
effigy Posted June 9, 2008 Share Posted June 9, 2008 This appears OK. Is a certain string giving you trouble? P.S. You can condense the youtube and img patterns to /\[(img|youtube)\](.*?)\[\/\1\]/i. Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 Well when I add a matches array and print it I get Array ( [0] => \n012345678901234567890123456789\n0123456789 ) Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks [The string I entered in the form is all nummeric to test, but as you can see it still catches the newlines] code <?php if (preg_match ('/[^\s]{41,}/i', $tempString, $matches)) { print_r ($matches); $errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks'; } ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted June 9, 2008 Share Posted June 9, 2008 That's what you want, right? That means it is being detected. Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 No I want a string that has NO spaces and NO newlines =/ I also tried [^\s\n] but it had the same effect I tried just about anything :s [^(\n|\s)] [^([\n]|[\s])] .... all with no effect Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 <?php $tempString = str_replace ('\r', '', $tempString); $tempString = str_replace ('\n', '[newline]', $tempString); $tempString = str_replace ('\s', '[space]', $tempString); if (preg_match ('/[^(\[newline\]|\[space\])]{41,}/i', $tempString, $matches)) { print_r ($matches); $errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks'; } ?> this does work, which is weird Quote Link to comment Share on other sites More sharing options...
effigy Posted June 9, 2008 Share Posted June 9, 2008 <pre> <?php $tests = array( "0123456789\n0123456789\n0123456789\n0123456789\n0123456789\n0123456789", 'abc def ghi jkl mnopqrstu', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'ccccccccccccccccccccccccccccccccccccccccccc', ); foreach ($tests as $test) { echo $test, '<br><b>'; echo preg_match('/\S{41,}/', $test) ? 'Overflow!' : 'OK' ; echo '</b><hr>'; } ?> </pre> 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 OK abc def ghi jkl mnopqrstu OK aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Overflow! bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb OK ccccccccccccccccccccccccccccccccccccccccccc Overflow! Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 yes but try adding 40 characters, then a new line and then another 40 chars Quote Link to comment Share on other sites More sharing options...
effigy Posted June 9, 2008 Share Posted June 9, 2008 You were using 41 in your pattern, so 40 will be OK. The pattern will need to be changed. Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 I don't know why but your way doesn't work for me :s What I mean is, if you enter EXACTLY 40 characters and then add a newline through the form, it won't match <?php $tempString = str_replace ('\n', '[::&newline&::]', $tempString); if (preg_match ('/[^(\[\:\:\&newline\&\:\:\]|\s)]{41,}/i', $tempString)) { $errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks'; } ?> When I do it like that it works strangely enough, but if I just use \n it still counts the newline character to the string, making it 42 Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted June 9, 2008 Author Share Posted June 9, 2008 I fixed the problem by just changing the newlines to spaces and just checking for spaces Quote Link to comment Share on other sites More sharing options...
effigy Posted June 10, 2008 Share Posted June 10, 2008 What does this give you? <pre> <?php $tests = array( str_repeat('a', 40) . "\n" . str_repeat('a', 40), str_repeat('a', 41) . "\n" . str_repeat('a', 41), str_repeat('a', 42) . "\n" . str_repeat('a', 42), ); foreach ($tests as $test) { echo $test, '<br><b>'; echo preg_match('/\S{41,}/', $test) ? 'Overflow!' : 'OK' ; echo '</b><hr>'; } ?> </pre> I get: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa OK aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Overflow! aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Overflow! You pattern will not work. Character classes ([...]) are only a list of characters from which one is picked, unless a quantifier is applied. It will not match strings of characters contained within. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.