papaface Posted September 15, 2007 Share Posted September 15, 2007 Hello, How do I check if a string contains something other than \n. E.g If $string has "\n" in it, it will be rejected. But if it has "\nhello" in it that will be okay. Thanks Quote Link to comment Share on other sites More sharing options...
jscix Posted September 15, 2007 Share Posted September 15, 2007 if the string contains just \n, the STRLEN($string), of that string should be 2. if it's greater than two, it contains other character, or. or you could find the last position of \n, get all the text before and after it, and make sure each one has a value besides "" etc. Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 Oh sorry I thought it was for file parsing, so: $s = "\n"; $n = strlen($s); echo "len: ".$n."<br>"; $s = trim($s); $n = strlen($s); echo "len: ".$n."<br>"; e.g. trim get's rid of '\n', but that won't work in your case... if(strstr($s, "\n") && (strlen($s) > 1)) Should work but not tried, other checks may be needed... i.e. if not contain! Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 The length of "\n" should be 1, not 2. The '\' is a switch and ignored, ascii char 10... Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 15, 2007 Share Posted September 15, 2007 why not use trim() and then check the strlen? Quote Link to comment Share on other sites More sharing options...
papaface Posted September 15, 2007 Author Share Posted September 15, 2007 Thanks for your advice people At first I thought the \n and \rs were still in the string but they werent. so i just used: if (strlen($value) == 0) { unset($domain[$key]); } Instead. Thanks Quote Link to comment Share on other sites More sharing options...
corbin Posted September 15, 2007 Share Posted September 15, 2007 Just for the record, you could do something like this if you did want to find if \n was in a string. $l1 = strlen($str); $l2 = strlen(str_replace("\n", $str)); if($l1 != $l2) echo 'String contains \n'; 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.