count newlines in heredoc variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Cantrell

    count newlines in heredoc variable

    How could I count the number of newlines in a heredoc variable? This
    outputs 0, but I thought heredoc preserved newlines?

    <?
    $test = <<<END
    <a href="test5.php ">adam</a>
    <p>
    Hi There
    </p>
    END;

    echo substr_count($t est, '\n');
    ?>
  • petersprc

    #2
    Re: count newlines in heredoc variable

    Hi,

    You're correct, but you just need double quotes: "\n".

    In a single-quoted string, the only interpreted escape sequences are \
    \ and \'.

    Regards,

    John Peters

    On Jul 30, 2:21 pm, Adam Cantrell <cantrel...@gma il.comwrote:
    How could I count the number of newlines in a heredoc variable? This
    outputs 0, but I thought heredoc preserved newlines?
    >
    <?
    $test = <<<END
    <a href="test5.php ">adam</a>
    <p>
    Hi There
    </p>
    END;
    >
    echo substr_count($t est, '\n');
    ?>

    Comment

    • Adam Cantrell

      #3
      Re: count newlines in heredoc variable


      Thank you very much -

      Adam Cantrell

      On Jul 30, 11:48 am, petersprc <peters...@gmai l.comwrote:
      Hi,
      >
      You're correct, but you just need double quotes: "\n".
      >
      In a single-quoted string, the only interpreted escape sequences are \
      \ and \'.
      >
      Regards,
      >
      John Peters
      >
      On Jul 30, 2:21 pm, Adam Cantrell <cantrel...@gma il.comwrote:
      >
      How could I count the number of newlines in a heredoc variable? This
      outputs 0, but I thought heredoc preserved newlines?
      >
      <?
      $test = <<<END
           <a href="test5.php ">adam</a>
           <p>
                Hi There
           </p>
      END;
      >
      echo substr_count($t est, '\n');
      ?>
      >
      >

      Comment

      Working...