Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    testing for new line and tag characters in strings?

    Is this the best way to represent new line/(carriage return) and tab characters?
    PHP Code:
    $_txtScreen '+=()*&^%$#@!~\`"\'/<>|{}[]'."\n"."\r"."\t"."\n\r"
    I want to screen for new lines and tabs in $_POST vars input.
    I expect that they would only occur in input from textArea fields, normally.

    Then escaping them properly is my problem. I assume they would be url encoded by the browser and url unencoded by php.

    So testing I am using === to test equality and escaping
    with '\'.(new line or tab char).'\'; as
    PHP Code:
    if($_char === "\n")
      {
       
    $_char "'\'".$char// I want a literal representation here: \n, ideally
      

    Alternative is to convert from functional representation to literal representation, but I am not sure how to do that. Or, maybe I don't
    need to if is already rendered literal by php in the process of url unencode.

    Thanks for time and attention.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Comparative input would best be done using PHP_EOL constant. That should match the linefeed used by the OS, and should work with any explicit entry. It may not work properly with something from say a file, for which you should use a method that detects linefeeds like fgets or file.
    You can pull the numeric representation of a char using ord(). chr() is the reverse, so \n would be reprsented as chr(10);.
    As for patterns, pcre should have a linefeed which I believe is \v. I'm not 100% sure if this runs via the same concept as PHP_EOL or if it actually tries the combinations of \r and \n.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    what I am doing

    The $_txtScreen variable is used in a loop testing each character in a
    string against this value
    PHP Code:
    if(strpos($_txtScreen$_char[loop iterator]) > -1)
      {
       
    // the char is found in this list of characters
      // add it to error accumulation string (so I want the literal version of \n of \n\r)

    In the php manuals and various texts I have been using, it is suggested
    to avoid using regex, as being slower than string processing functions.
    So I am designing everything to avoid as much as possible, the use
    of regex.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •