Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

Detecting Carriage Return

Hi Experts,

I have a MYSQL field with varchar(4000) for comments, which I am storing in a php variable using PDO.

I need to emphasize on the first paragraph, in other words until the first carriage return. My question is how can I detect carriage return?

Thank you
Avatar of F P
F P
Flag of United States of America image

Using double quotes is the easiest way and fastest method would be strpos() with it. BTW, Linux and Windows have different returns. Windows is the R below and *nix is the N

<?php

$found_nix = (strpos($haystack, "\n") !== FALSE ?);
$found_win = (strpos($haystack, "\r") !== FALSE ?);

Open in new window


Don't do them together since it might miss only having one or the other, and use strong type checking on the strpos because it could be the first character making it's value 0 for a positive answer. Both those variables return a boolean type.
Your fancy way is with regular expressions. Here's that...

preg_match('/(\r?\n){2}/', $subject, $matches);
print_r($matches);

Open in new window


If it's found the array is populated with the matching values to the $matches[] array variable. If not, it's empty.
Avatar of APD Toronto

ASKER

Based on your example, I came up with the following, but I get empty sting on my echo, and only the first character of $comment

            $newline_pos = -1;
            if ($env['server_os'] == 'Windows'){
                $newline_pos = strpos($comment, '\n');
            } else {
                $newline_pos = strpos($comment, '\r');
            }
            echo '<br> pos = '. $newline_pos . '<br>';
            $comment = substr($comment, 0, $newline_pos + 1);
            

Open in new window

FYI I am developing on Windows and uploading on Linux
Your code is backwards. If it's windows you want it to be \r and if it's linux it should be \n. Also check for both either way because you can encode \r on linux and \n in windows. The breaks still exist, but might not be recognized when you open it to visually look at.
Also would not this:

$comment = substr($comment, 0, $newline_pos + 1)

in your code say to start at position 0 and end at position 0? A substring of nothing? You never change the value of newline_pos to +1 over the initial -1 you set it as.
My -1 is just a preset, I am actually getting a real value within if...else block.

At any rate, I switched the \r and \n, but I am still getting no value for $newline_pos
That makes sense though, because if it's the first character in the string, the position is 0. You're adding 0 to it...
ASKER CERTIFIED SOLUTION
Avatar of F P
F P
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but $newline_pos = strpos($comment, '\r'); on Windows still returns blank.  Why?

In my example, my carriage is not as position 0.
output is

bool(false)
I don't doubt your statement about it not being position 0, but I have to ask... How did you verify that the invisible character of the carriage return is not present? Unless you opened the content in a text editor and viewed invisible characters, it could still exist since they're invisible by default...

When you say it returns blank. PHP will echo VOID. What's that mean? If the value is NULL, FALSE, or and empty string, it will still echo out nothing and be blank. We don't know if it's actually NULL, or empty, until you do a var_dump. The return on strpos is either an integer, which could be 0, or boolean FALSE if it isn't found. I just want to be clear what the values and types are since I can't duplicate it myself.
But you're checking with your code for a different value when on Windows, correct? Isn't the value of your variable the same if you do the same check that is returning as present on linux? i.e., what I wrote before to check it for both values regardless of system type.
I'm entering/checking this that the string has a carriage return via a <textarea>, please see attached.

I can produce a little example in a few min.
If you save the following under text.php(form's action) then submit, you will get the same results, but you will see that "Line 2" is clearly after a carriage return (2 actually)

<?php
    if (isset($_POST['txtComment'])){
        $comment = $_POST['txtComment'];
        echo $comment;            
        $newline_pos = strpos($comment, '\n');

        if($newline_pos === 0)
        {
            $newline_pos = strpos($comment, '\r');
        }

        echo '<br><br>' ;
        var_dump($newline_pos);
    }

?>

<html>
<head>
    <title>test</title>
    <style>
        textarea{
            width:1000px;
            height: 500px;
        }
    </style>
</head>
<body onload="loadAgents();">
    
    <form method="post" action="test.php">
        
        <textarea name="txtComment">Aleks TESTing a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long comment...
Line 2</textarea>
        
        <input type="submit">
        
    </form>
  
</body>
</html>

                                          

Open in new window

SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good eye Dave! Maybe I should decrease my resolution.
BTW, don't use isset(). Use this:

array_key_exists('txtComment', $_POST);

Open in new window

Thank you both... One issue is down, one more to go!
When you want the end-of-line characters, you can use PHP_EOL.  PHP_EOL is a predefined PHP constant that is context-aware, so it knows what the end-of-line character looks like in Windows, Unix, etc.  This will always be more dependable than coding explicity \r, \n, or \r\n sequences.  And it will be portable between systems.
Thank you for the tip Ray.