Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2012, 03:26 AM   PM User | #1
anotherJEK
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: 150
Thanks: 41
Thanked 1 Time in 1 Post
anotherJEK is an unknown quantity at this point
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.
anotherJEK is offline   Reply With Quote
Old 04-14-2012, 06:53 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 04-14-2012, 09:55 PM   PM User | #3
anotherJEK
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: 150
Thanks: 41
Thanked 1 Time in 1 Post
anotherJEK is an unknown quantity at this point
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.
anotherJEK is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:50 PM.


Advertisement
Log in to turn off these ads.