Author Topic: [SOLVED] Check if a string contains something other than \n  (Read 496 times)

0 Members and 1 Guest are viewing this topic.

Offline papafaceTopic starter

  • Devotee
  • Posts: 1,435
  • I'll try to help :)
    • View Profile
[SOLVED] Check if a string contains something other than \n
« on: September 15, 2007, 04:46:27 PM »
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 :D
The best web hosting at http://www.5ite.com!

Offline jscix

  • Enthusiast
  • Posts: 197
    • View Profile
Re: Check if a string contains something other than \n
« Reply #1 on: September 15, 2007, 04:52:42 PM »
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.

Offline rarebit

  • Devotee
  • Posts: 996
    • View Profile
Re: Check if a string contains something other than \n
« Reply #2 on: September 15, 2007, 04:53:58 PM »
Oh sorry I thought it was for file parsing, so:
Code: [Select]
$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...

Code: [Select]
if(strstr($s, "\n") && (strlen($s) > 1))Should work but not tried, other checks may be needed... i.e. if not contain!

Offline rarebit

  • Devotee
  • Posts: 996
    • View Profile
Re: Check if a string contains something other than \n
« Reply #3 on: September 15, 2007, 04:55:34 PM »
The length of "\n" should be 1, not 2. The '\' is a switch and ignored, ascii char 10...

Offline jesirose

  • Fanatic
  • Posts: 3,819
  • Gender: Female
  • PHP Goddess
    • View Profile
    • Reed's Training Blog
Re: Check if a string contains something other than \n
« Reply #4 on: September 15, 2007, 04:56:29 PM »
why not use trim() and then check the strlen?
Every time you post "It didn't work" without more explanation, God kills a kitten.
When you post code without code tags, He just teases a puppy. But it's still sad.
What does that php function do? | What does that term mean? | I don't see any errors! | Awesome API Interface
PHP Goddess with Attitude since 2004

Offline papafaceTopic starter

  • Devotee
  • Posts: 1,435
  • I'll try to help :)
    • View Profile
Re: Check if a string contains something other than \n
« Reply #5 on: September 15, 2007, 05:04:38 PM »
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:
Code: [Select]
if (strlen($value) == 0)
{
unset($domain[$key]);
}
Instead.

Thanks :D
The best web hosting at http://www.5ite.com!

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: [SOLVED] Check if a string contains something other than \n
« Reply #6 on: September 15, 2007, 06:25:15 PM »
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';
Why doesn't anyone ever say hi, hey, or whad up world?