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
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

F PCommented:
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.
F PCommented:
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.
APD TorontoSoftware DeveloperAuthor Commented:
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

APD TorontoSoftware DeveloperAuthor Commented:
FYI I am developing on Windows and uploading on Linux
F PCommented:
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.
F PCommented:
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.
APD TorontoSoftware DeveloperAuthor Commented:
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
F PCommented:
That makes sense though, because if it's the first character in the string, the position is 0. You're adding 0 to it...
F PCommented:
 
// environment makes no difference on what the text evaluated contains once it's written to a db or file.
$newline_pos = strpos($comment, '\n');

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

var_dump($newline_pos);
exit;

Open in new window


What is that output?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
but $newline_pos = strpos($comment, '\r'); on Windows still returns blank.  Why?

In my example, my carriage is not as position 0.
APD TorontoSoftware DeveloperAuthor Commented:
output is

bool(false)
F PCommented:
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.
F PCommented:
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.
APD TorontoSoftware DeveloperAuthor Commented:
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.
APD TorontoSoftware DeveloperAuthor Commented:
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

Dave BaldwinFixer of ProblemsCommented:
Your first problem is that '\n' will not work because single quoted strings do not convert escaped characters.  It would have to be "\n" to be interpreted as a newline and "\r" to be interpreted as a carriage return.

http://php.net/manual/en/language.types.string.php

You could also use the 'chr' function:  http://php.net/manual/en/function.chr.php
F PCommented:
Good eye Dave! Maybe I should decrease my resolution.
F PCommented:
BTW, don't use isset(). Use this:

array_key_exists('txtComment', $_POST);

Open in new window

APD TorontoSoftware DeveloperAuthor Commented:
Thank you both... One issue is down, one more to go!
Ray PaseurCommented:
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.
APD TorontoSoftware DeveloperAuthor Commented:
Thank you for the tip Ray.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.