
PHP HTML Functions – जब हम PHP को MySQL के साथ Use करते हैं, तो PHP हमें कई ऐसे Functions Provide करता है, जिनकी हमें विशेष स्थितियों को Handle करने के लिए जरूरत पडती है। ये Functions मूल रूप से HTML Form से आने वाले Data को MySQL के साथ बेहतर तरीके से Use करने की सुविधा देते हैं। PHP द्वारा Database Handling के समय Use कुछ बहुत ही उपयोगी Functions निम्नानुसार हैं:
addcslashes() Function
इस Function का Syntax निम्नानुसार होता हैः
string addcslashes(string $str, string $charlist)
इस Function में पहले Argument के रूप में हमें एक String Specify करना होता है, जबकि दूसरे Parameter के रूप में हमें Characters की एक List को Specify करना होता है। दूसरे Parameter में हम Characters की जिस List को Specify करते हैं, Function Execute होने के बाद उन Characters के साथ एक “C” Style Backslash Character Place हो जाता है।
चूंकि ये Function “C” Style Slash Add करता है, इसलिए यदि हम n या r जैसे Characters को दूसरे Parameter के रूप में Specify करते हैं, तो ये उन्हें Function \n या \r में Convert कर देता है, जो कि Newline व Carriage Return Character Constants को Represent करते हैं। जबकि अन्य Non-Alphanumerical Characters जिनका ASCII Code 32 से कम व 126 से ज्यादा होता है, के Characters को ये Function Octal Representation में Concert कर देता है।
जब हम दूसरे Parameter में Characters की एक Sequence को Specify करते हैं, तो हमें Starting व Ending Character की Range को Represent करने के लिए उनके बीच Double Dots का प्रयोग करना पडता है। जैसेः
<?php echo addcslashes('Hello!', 'A..z'); //Output: \H\e\l\l\o\! // All upper and lower-case letters will be escaped but so will the [\]^_` ?>
यदि Range में Specify किए गए पहले Character की ASCII Value दूसरे Character की ASCII Value से ज्यादा हो, तो कोई Range Crete नहीं होती है। बल्कि केवल Starting व Ending Character तथा Dots ही Escape होते हैं। जैसेः
<?php echo addcslashes('zoo!', 'z..A'); //Output: \zoo['\.'] ?>
जब हम 0, a, b, f, n, r, t व v Characters को Escape करते हैं, तो ये Character Special Character Constants की तरह व्यवहार करने लगते हैं, क्योंकि \n “C” Style में New Line को Represent करता है जबकि /0 NULL को।
ये Function Execute होने के बाद Escaped Characters की String Return करता है।
stripcslashes() Function
इस Function का Syntax निम्नानुसार होता हैः
string stripcslashes(string $str)
ये Function addcslashes() Function का Exactly Reverse काम करता है। यानी addcslashes() Function द्वारा जिस String को Escape किया गया होता है, उस String को ये Function फिर से Normal String में Convert कर देता है।
addslashes() Function
इस Function का Syntax निम्नानुसार होता हैः
string addslashes(string $str)
ये Function Argument के रूप में Pass की गई String में जिन Characters को Escape करने की जरूरत होती है, जैसे कि Single Quotes, Double Quotes, Backslash, व NUL, उन्हें Escape करके Return करता है।
उदाहरण के लिए यदि हम किसी Database में INSERT Query द्वारा “Hello! What’s up” String को Database में Insert करना चाहते हैं, तो हमें पहले इस String को addslashes() Function में Pass करना होता है और Return होने वाली Modified String को Database में Store करना होता है, क्योंकि इस String में Specified Single Quotes का MySQL Database के लिए Special Meaning होता है।
सामान्यतः इस Function को Use करने के स्थान पर mysqli_real_escape_string() Function को Use किया जाता है, क्योंकि ये Function addslashes() Function की तुलना में ज्यादा बेहतर तरीके से काम करता है।
<?php $str = "Hello! What’s up?"; // Outputs: Hello! What\’s up?? echo addslashes($str); ?>
stripslashes() Function
इस Function का Syntax निम्नानुसार होता हैः
string stripcslashes(string $str)
ये Function addslashes() Function का Exactly Reverse काम करता है। यानी addslashes() Function द्वारा जिस String को Escape किया गया होता है, उस String को ये Function फिर से Normal String में Convert कर देता है।
htmlspecialchars() Function
इस Function का Syntax निम्नानुसार होता हैः
string htmlentities(string $str, int $flags, string $charset, bool $double_encode=true)
ये Function String के विभिन्न Special Characters को HTML Entities में Convert कर देता है। उदाहरण के लिए HTML में कुछ Characters Special Significance Characters होते हैं, जिन्हें HTML Entities की तरह ही Represent करना जरूरी होता है, अन्यथा Web Browser HTML Web Page के Codes को String में Specified Characters के आधार पर Modify कर देता है, जिससे HTML Web Page की Normal Rendering में परेशानी आती है, क्योंकि Web Browser String में Specified इन Characters के आधार पर HTML Tags की Opening व Closing को Identify कर लेता है।
इसलिए किसी String में HTML Tags व Special Characters जैसे कि <, > आदि को HTML Entities के रूप में Render करना जरूरी होता है और इस जरूरत को इस Function द्वारा पूरा किया जाता है।
इस Function का प्रयोग HTML Form में User द्वारा Supplied HTML Markups को Database में Insert होने या Web Page पर Render होने से रोकने के लिए किया जाता है। ये Function निम्न HTML Entities का Conversion करता हैः
- & को & में Convert कर देता है।
- ” को &quo; में Convert कर देता है।
- ‘ को ' में Convert कर देता है।
- < को < में Convert कर देता है।
- > को > में Convert कर देता है।
इस Function के पहले Parameter के रूप में हमें उस String को Specify करना होता है, जिस पर हम इस Function को Apply करना चाहते हैं।
दूसरे Parameter के रूप में हम हमारी जरूरत के अनुसार निम्न में से किसी एक या Bitwise OR Operator का प्रयोग करते हुए एक से ज्यादा Constant मानों को Specify कर सकते हैं:
ENT_COMPACT Constant
ये मान Double Quotes को तो Convert करता है, लेकिन Single Quotes को Convert नहीं करता।
ENT_QUOTES Constant
ये मान दोनों Quotes को Convert करता है।
ENT_NOQUOTES Constant
ये मान दोनों Quotes को Convert नहीं करता है।
ENT_IGNORE Constant
ये मान Empty String Return करने के स्थान पर Invalid Code Unit Sequence को Discard कर देता है। इसे उपयोग में नहीं लेना चाहिए, क्योंकि इसे केवल Backward Compatibility के लिए ही PHP में Included रखा गया है।
ENT_SUBSTITUTE Constant
ये मान Empty String Return करने के स्थान पर U+FFFD (UTF-8) या &#FFFD; Return करता है।
ENT_DISALLOWED Constant
ये मान Empty String Return करने के स्थान पर Invalid Code Unit Sequence के स्थान पर U+FFFD (UTF-8) या &#FFFD; Return करता है।
ENT_HTML401 Constant
ये मान HTML Entities Code को HTML 4.01 के अनुसार Treat करता है।
ENT_XML1 Constant
ये मान HTML Entities Code को XML 1 के अनुसार Treat करता है।
ENT_HTML5 Constant
ये मान HTML Entities Code को HTML5 के अनुसार Treat करता है।
इस Function के तीसरे Parameter में हम Conversion के लिए Use किए जाने वाले Characterset को Specify करते हैं। जबकि चौथे Parameter में false Specify करके हम String में पहले से Encoded HTML Entities को फिर से दुबारा Encode करने से रोक सकते हैं।
सामान्य स्थिति में ये Function Encoded String Return करता है, जबकि यदि पहले Parameter में Invalid Code Unit Sequence हो तथा ENT_IGNORE Flag को Set न किया गया हो, तो ये Function एक Empty String Return करता है। इस Function को हम निम्नानुसार Use कर सकते हैं:
<?php $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); echo $new; // <a href='test'>Test</a> ?>
htmlspecialchars_decode() Function
इस Function का Syntax निम्नानुसार होता हैः
string htmlspecialchars_decode(string $str, int $quotestyle=ENT_COMPACT)
ये Function htmlspecialchars() Function का Exactly Reverse काम करता है। यानी ये Function सभी HTML Entities को उनसे संबंधित Applicable Characters में Convert कर देता है। इसे हम निम्नानुसार Use कर सकते हैं:
<?php $str = '<p>this -> "</p>'; echo htmlspecialchars_decode($str); // note that here the quotes aren't converted echo htmlspecialchars_decode($str, ENT_NOQUOTES); ?>
htmlentities() Function
इस Function का Syntax निम्नानुसार होता हैः
string htmlentities(string $str, int $flags, string $charset, bool $double_encode=true)
ये Function Exactly htmlspecialchars() Function की तरह ही काम करता है। अन्तर केवल इतना है कि htmlspecialchars() Function केवल &, ‘, “, < व > को ही Encode करता है, जबकि htmlentities() Function String के सभी Characters को HTML Entities में Convert कर देता है। जैसेः
<?php $str = "A 'quote' is <b>bold</b>"; // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str); // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str, ENT_QUOTES); ?>
html_entity_decode() Function
इस Function का Syntax निम्नानुसार होता हैः
string html_entity_decode(string $str, int $flags, string $charset, bool $double_encode=true)
ये Function htmlentities() Function का Exactly Reverse काम करता है। यानी ये Function सभी HTML Entities को उनसे संबंधित Applicable Characters में Convert कर देता है। इसे हम निम्नानुसार Use कर सकते हैं:
<?php $orig = "I'll \"walk\" the <b>dog</b> now"; $a = htmlentities($orig); $b = html_entity_decode($a); echo $a; // I'll "walk" the <b>dog</b> now echo $b; // I'll "walk" the <b>dog</b> now ?>
nl2br() Function
इस Function का Syntax निम्नानुसार होता हैः
string nl2br(string $str, bool $is_xhtml=true)
ये Function Argument के रूप में एक String को Accept करता है व Return Value के रूप में सभी Newline Characters को HTML के <br/> Element में Convert करके Return करता है। इसे हम निम्नानुसार Use कर सकते हैं:
<?php echo nl2br("Welcome\r\nThis is my HTML document", false); ?> //Output: Welcome<br/> This is my HTML document
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook PHP in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
PHP in Hindi | Page: 647 | Format: PDF