tags:

views:

54

answers:

4

I need to detect more than one \n. Doesn't matter if it's 2 or 1000, as long as it's more than one \n. What would be the regex for this (if regex is necessary that is)?

EDIT

So I am using this:

$pregmatch = preg_match('#\\n\\n+#', $locations);
if ($pregmatch > 0) {
    echo 'more than one, this many: '.count($pregmatch);
} else
        echo 'less than one';

but count($pregmatch) doesn't return the actual number of more than one \n detected. How can that be achieved?

+2  A: 

Are you looking for more than 1 \n in general? if so:

if (preg_match_all('#\\n#', $string, $matches) > 1) {
    //More than 1 \n
}

Or without regex:

if (substr_count($string, "\n") > 1) {
    //More than 1 \n
}

Or even (but it's far less efficient):

$chars = count_chars($string);
if (isset($chars[ord("\n")]) && $chars[ord("\n")] > 1) {
    //More than 1 \n
}

If in a row:

if (preg_match_all('#\\n\\n+#', $string, $matches) > 0) {
    //More than 1 \\n in a row
}

Edit: So, based on your edit, I can summize two possibilities about what you want to know.

If you want to know the number of \n characters in a row (more than 1), you could do:

if (preg_match('#\\n\\n+#', $string, $match)) {
    echo strlen($match[0]) . ' Consecutive \\n Characters Found!';
}

Or, if you wanted to know for each occurance:

if (preg_match_all('#\\n\\n+#', $string, $matches)) {
    echo count($matches) . ' Total \\n\\n+ Matches Found';
    foreach ($matches[0] as $key => $match) {
        echo 'Match ' . $key . ' Has ' . 
            strlen($match) . ' Consecutive \\n Characters Found!';
    }
}
ircmaxell
A line break is not necessarily `\n`.
Tomalak
Is `'#\\n#'` really correct?
nikic
Yes it is. A line break is `\n`. Sure, some operating systems use `\r\n` (or just `\r`, but far less common in recent times), but that's the new line character. Considering that the original question was asking specifically about `\n` and the only reference to "line break" is in the title, I think the question is more about `\n` than generic line ending sequences. (also note how I didn't use the term "line break" in my answer). If the question is about line termination characters in general, I'll be happy to edit my answer. But it seems to me to be about `\n` specifically...
ircmaxell
@nikic: why not? I always escape all `\ ` out of habit... So it's identical (for single quotes) to: `#\n#`... And I prefer to use the `#` delimiter, since I find the character comes up far less that the other common ones `/`, etc)...
ircmaxell
@Daniel: Good point about `preg_match`, I've updated my answer. And no, `'\\n'` will result in a string with a literal `\n`. Escaping `\ ` with single quotes is not necessary (unless it's followed by a `'` character), but I do it all the time. I prefer the more verbose syntax, since I think it's easier to read (since in all cases a `\\ ` will result in a literal `\ ` in the string)... Just my preference... Try it. Build a string with single quotes and unescaped `\ ` characters, and one with escaped `\ ` characters. `var_dump` them and compare them. They will be the same (for `'` anyway)
ircmaxell
@ircmaxell You're right, my bad.
Daniel Vandersluis
@ircmaxell Hi, could you see my EDIT in the original question. I asked an additional question based on your answer.
Magpue
@Magpue: Answer edited in...
ircmaxell
A: 

Something like (doc):

preg_match_all("[\n]*", $yourString, $outArray)

preg_match_all will return a count of how many there were, as well as the matches in the outArray.

kchau
A: 

You need to provide the /m modifier so that the scan spans more than on line:

if (preg_match('/\n\n/m', $str)) {
    echo "match";
} else {
    echo "no match";
}
Alex Howansky
The `m` modifier only affects the `^` (start of string) and `$` (end of string) meta-characters. If you're not using either, it's the same regex (Kinda like having `/[0-9]/i` doesn't affect the pattern)...
ircmaxell
Check out the docs on it (too late to edit my prior comment): [pcre pattern modifiers](http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php)
ircmaxell
A: 

You can avoid using regex, saving CPU, here is an elegant and tricky way:

$text_parts = explode("\n", $original_string);

Now you can join it replacing line breaks with something elese:

$new_string = implode("<br />", $text_parts);

I hope this helps

Mike
you can also use str_replace() function
Mike
or the function built for this exact task: [`nl2br`](http://us.php.net/manual/en/function.nl2br.php)...
ircmaxell
so true!!! I have just invented a wheel :) I think I need some coffeee :)
Mike