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 Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/ 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. Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/#findComment-349219 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! Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/#findComment-349220 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... Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/#findComment-349222 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? Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/#findComment-349223 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 Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/#findComment-349229 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'; Link to comment https://forums.phpfreaks.com/topic/69501-solved-check-if-a-string-contains-something-other-than-n/#findComment-349269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.