PHP String Functions – Printing Statements

PHP String Functions: String PHP का केवल एक Data Type ही नहीं है बल्कि String किसी भी Web Related Programming Language का मूल आधार होता है। कैसे?

आप जितनी भी Websites देखते हैं, उनमें Audio, Video, Images, Multimedia आदि सभी चीजें होती हैं लेकिन मूल रूप से हर Website के मूल आधार के रूप में सिर्फ और सिर्फ Text ही होते हैं।

मानलो कि किसी Website में Text ही न हो, केवल Video, Audio व Images ही हों, तो उस Web Site से आप कितनी Information प्राप्त कर सकते हैं व ऐसी Web Site के साथ आप कितना Interaction कर सकते हैं?

बिना Texts के किसी उपयोगी Web Site की कल्पना भी नहीं की जा सकती। क्योंकि Texts ही किसी भी Web Site का मूल आधार होते हैं और Texts को किसी भी Programming Language में String के रूप में Identify किया जाता है। इसलिए String व PHP द्वारा Default रूप से Provide किए जाने वाले String Related Functions को बेहतर तरीके से समझना व उपयोग में लेना सीखना काफी जरूरी है।

किसी भी Text को Display करने के लिए PHP हमें कुछ विशेष Statements व Functions Provide करता है। EchoPrint Statements को हम पहले भी देख चुके हैं और अभी तक Use करते आए हैं।

इनके अलावा भी PHP हमें कुछ और Output Statements Provide करता है, जो कि विभिन्न तरीकों से व ज्यादा Controlling के साथ Strings को Output करने की सुविधा प्रदान करते हैं। तो सबसे पहले हम इन्हीं Output Statements के बारे में जानेंगे।

printf() Function

ये Function “C” Language के printf() Function से ही Inspired है और Exactly उसी तरह से काम करता है, जिस तरह से “C” Language में करता है। इस Function का प्रयोग करके किसी String को तब Output में Render किया जाता है, जब हम String को विशेष Formatting के साथ Print करना चाहते हैं।

ये Function एक String के साथ Variable Number of Arguments Accept करता है और Argument के रूप में Specified Identifiers के मानों को String के बीच उन स्थानों पर Expand करता है, जिन स्थानों पर इन्हें Display करने के लिए हम विभिन्न Formatting Options को Specify करते हैं। इन Formatting Options “C” Language में Control String के नाम से भी जाना जाता है। इस Function का Syntax निम्नानुसार होता हैः

int printf (string $format, mixed $args, mixed $…)

ये Function Return Value के रूप में Output होने वाली यानी Screen पर Display होने वाली String की Length Return करता है।

जब हम इस Function को Use करके किसी String या Text को Output करना चाहते हैं, तब सामान्यतः String के बीच किसी एक या एक से अधिक Identifiers के मानों को Specify करने के लिए हमें विभिन्न प्रकार के Format Modifiers यानी Control Strings का प्रयोग करना पडता है।

इन Control Strings को “C” Language की तरह ही % Sign के साथ Specify करना होता है। इसलिए जब कभी भी हम printf() Function को Use करते हैं और String के बीच % Sign को भी Output करना चाहते हैं, तब हमें % Sign को दो बार यानी %% लिखना पडता है क्योंकि एक % Sign को printf() Function Control String या Format Modifier के रूप में Identify करता है।

हम Output होने वाली String के बीच जिस स्थान पर किसी Identifier के मान को Place करना चाहते हैं, उस स्थान पर हमें Identifier के Data Type के Control String या Format Modifier को Use करना होता है और String के Double Quotes के बाद Comma Separator का प्रयोग करके हमें उस Identifier के नाम को Specify करना होता है, जिसके मान को हम String में Places Format Modifier के स्थान पर Place करना चाहते हैं।

उदाहरण के लिए मानलो कि किसी Variable में कोई नाम Stored है और हम उस नाम को printf() Function द्वारा Output करना चाहते हैं, इस जरूरत को पूरा करने के लिए हमें printf() Function को निम्नानुसार Use करना होगाः

<?php
	$name = "Man Mohana";
	
	printf("Name : %s", $name);
?>

जब ये PHP Program Run होता है, जब हमें निम्नानुसार Output प्राप्त होता हैः

Name : Man Mohana

जैसाकि हम उपरोक्त printf() Function में देख सकते हैं कि Double Quotes के बीच हमने वो String Specify की है, जिसे हम Output करना चाहते हैं तथा जिस स्थान पर हम Variable Identifier $name के मान को Display करना चाहते हैं, String में उस स्थान पर हमने %s Format Modifier या Control String का प्रयोग किया है, क्योंकि String को Print करने के लिए हमें %s Modifier का प्रयोग करना होता है।

यदि हम चाहें तो इस Function में एक से ज्यादा Modifiers को भी अपनी जरूरत के अनुसार Specify कर सकते हैं और विभिन्न प्रकार के Output प्राप्त कर सकते हैं। इस Concept को समझने के लिए हम उपरोक्त Program को ही थोडा सा Modify कर रहे हैं। हमारा Modified Program निम्नानुसार हैः

<?php
	$name = "Man Mohana";
	const VOTINGAGE = 18;
	
	printf(" Name : %s \n Voting Age: %d", $name, VOTINGAGE);
?>

जैसाकि उपरोक्त Program के printf() Function के Color Code द्वारा हम समझ सकते हैं कि जिस स्थान पर हमने %s Format Modifier का प्रयोग किया है, उस स्थान पर %name Variable Identifier का मान Replace हो जाता है जबकि जिस स्थान पर हमने %d Format Modifier का प्रयोग किया है, उस स्थान पर Constant Identifier का मान Replace हो जाता है। परिणामस्वरूप इस printf() Function के Run होने पर हमें निम्नानुसार Output प्राप्त होता हैः

//Output

Name : Man Mohana
Voting Age: 18

विभिन्न प्रकार के Data को इस Function द्वारा Print करने के लिए हमें विभिन्न प्रकार के Format Modifiers की जरूरत होती है। इस Function के साथ Use किए जा सकने वाले विभिन्न प्रकार के Modifiers या Type Specifies निम्नानुसार हैं:

Specifier Meaning
b The argument is an integer and is displayed as a binary number.
c The argument is an integer and is displayed as the character with that value.
d The argument is an integer and is displayed as a decimal number.
f The argument is a double and is displayed as a floating-point number.
e The argument is a double and is displayed as a floating-point exponential number with Lowercase ‘e’.
E The argument is a double and is displayed as a floating-point exponential number with Uppercase ‘E’.
g The argument is a double with precision and is displayed as a floating-point and exponential number with Lowercase ‘e’
g The argument is a double with precision and is displayed as a floating-point and exponential number with Uppercase ‘E’
o The argument is an integer and is displayed as an octal (base-8) number.
s The argument is a string and is displayed as such.
u The argument is an unsigned integer and is displayed as a decimal number.
x The argument is an integer and is displayed as a hexadecimal (base-16) number; lowercase letters are used.
X The argument is an integer and is displayed as a hexadecimal (base-16) number; uppercase letters are used.

इन सभी Type Specifies के Behavior को समझने के लिए हम निम्नानुसार एक Program बना सकते हैं:

<?php
	printf("Binary of 100: %b \n", 100);
	printf("Character of 100: %c \n", 100);
	printf("Decimal of 100: %d \n", 100);

	printf("Double of 100: %e \n", 100);
	printf("Double of 100: %f \n", 100);
	printf("Double of 100: %g \n", 100);
	
	printf("Octal of 100: %o \n", 100);
	printf("String of 100: %s \n", 100);
	printf("Unsigned of 100: %u \n", -100);
	
	printf("Hexadecimal of 100: %x \n", 100);
	printf("Hexadecimal of 100: %X \n", 100);
?>

//Output
Binary of 100			: 1100100
Character of 100		: d
Decimal of 100			: 100
Double of 100			: 1.000000e+2
Double of 100			: 100.000000
Double of 100			: 100
Octal of 100			: 144
String of 100			: 100
Unsigned of 100		: 4294967196
Hexadecimal of 100		: 64
Hexadecimal of 100		: 100

जिस तरह से “C” Language में विभिन्न प्रकार के Specifies का प्रयोग करके हम Output को Control कर सकते हैं, उसी तरह से हम PHP में भी Output को Control कर सकते हैं। जैसेः

printf(“[%10d]”, 100);

ये Statement Screen से कुल 10 Characters की Width Occupy करेगा और Value को Right Side से Display करेगा। परिणामस्वरूप Value निम्नानुसार दिखाई देगीः

[       100]

यहां Specify किया गया Underscore वास्तव में Space है।

printf(“[%’X10d]”, 100);

ये Statement निम्न Output Generate करेगाः

[XXXXXXX100]

ये Statement Screen से कुल 10 Characters की Width Occupy करेगा और Value को Right Side से Display करेगा। परिणामस्वरूप Value निम्नानुसार दिखाई देगीः

[0000000100]

चूंकि हमने % के बाद 0 Character को Use किया है, इसलिए Output में कुल 10 Characters में से 7 Characters के स्थानों पर 0 Fill हो जाएगा और द्रोष तीन स्थानों पर 100 Print होगा।

इस Function में जब हम + या – Sign का प्रयोग Format Specifier के साथ करते हैं, तब Numerical Values व String Values के लिए इनका Effect अलग-अलग होता है।

Numerical Value के साथ + का चिन्ह Use करने पर संख्‍या के साथ + Sign दिखाई देता है। जैसेः

<?php
	printf("\n [%+10d]", 100);
	printf("\n [%-10d]", 100);
?>

//Output
   [      +100]
   [100       ]

और String के साथ – का Sign Use करने पर String Left Justified Format में Display होती है, जबकि Default रूप से Width Specify करने पर String भी Right Justified Format में ही Print होता है।

<?php
	printf("\n [%10s]", 100);
	printf("\n [%+10s]", 100);
	printf("\n [%-10s]", 100);
?>

//Output
   [       100]
   [       100]
   [100       ]

दसमलव वाली संख्‍याओं के साथ भी हम Width Specifier का प्रयोग कर सकते हैं। दसमलव वाली संख्‍याओं के साथ Width Modifier का प्रयोग करने पर हमें दसमलव के बाद Print हो सकने वाली कुल Digits की संख्‍या को एक Dot के बाद Specify करना होता है जबकि कुल Width की संख्‍या को Dot से पहले Specify करना होता है। जैसेः

printf(“[%15.4f]”, 21.2);

ये Statement कुल 15 Digits की Space Reserve करेगा और Right Side से Printing करते हुए दसमलव के बाद कुल 4 Digits Display करेगा। परिणामस्वरूप हमें निम्न Output प्राप्त होगाः

[        21.2000]

यदि हम Blank Spaces के स्थान पर 0 Fill करना चाहें, तो हम उपरोक्त Statement को निम्नानुसार Modify भी कर सकते हैं:

printf(“[%015.4f]”, 21.2);

और हमारा Output अब निम्नानुसार होगाः

[0000000021.2000]

हम देख सकते हैं कि हमारी संख्‍या 21.2 में दसमलव के बाद केवल एक ही Digit है, इसलिए ये Function स्वयं दसमलव के बाद बाकी बचे हुए स्थानों को 0 से Fill कर देता है।

यदि हमारा Statement निम्नानुसार होताः

printf(“[%015.2f]”, 21.25566);

तो हमारा Output निम्नानुसार होताः

[0000000021.26]

हालांकि हमारे Statement में दसमलव के बाद 21.25566 यानी कुल 5 Digits हैं, लेकिन Format में हमने %015.2f लिखा है। जिसकी वजह से ये Function दसमलव के बाद की 5 Digits संख्‍याओं को Format में Specified संख्‍या यानी 2 Digits में Convert करने के लिए दसमलव के बाद की संख्‍याओं को Round-Off कर देता है।

sprintf() Function

printf() Function की तरह ही sprintf() Function भी समान प्रकार से Arguments Accept करता है और दोनों में हम समान प्रकार से Format Specifies व Width Specifies का प्रयोग कर सकते हैं।

लेकिन ये Function String को Output में Display नहीं करता, बल्कि Formatted String को Return करता है, जिसे हम किसी Variable में Store करके अपनी जरूरत के अनुसार Formatted String को कहीं भी Display कर सकते हैं। जैसेः

<?php
	$day = 10;
	$month = 2;
	$year = 2000; 
	$date = sprintf("%02d/%02d/%04d", $month, $day, $year);
	echo "Birth Date: " . $date;
?>

//Output
   Birth Date: 02/10/2000

vsprintf() Function

sprintf() Function की तरह ही vsprintf() Function भी समान प्रकार से Formatted String Generate करता है लेकिन Formatted String को Output में Display नहीं करता। इन दोनों Functions में केवल एक ही अन्तर है कि sprintf() Function Variable Number of Arguments Accept करता है जबकि vsprintf() Function दूसरे Argument के रूप में Arguments का एक Array Accept करता है।

इस Function से Return होने वाले Output को भी हम sprintf() Function की तरह ही किसी Variable में Store करके पूरे Program में किसी भी अन्य स्थान पर Display कर सकते हैं। जैसेः

<?php
	$day = 10;
	$month = 2;
	$year = 2000; 
	
	$date = vsprintf("%02d/%02d/%04d", array($month, $day, $year));
	echo "Birth Date: " . $date;
?>

//Output
   Birth Date: 02/10/2000

vprintf() Function

ये Function Exactly printf() Function की तरह काम करता है और Formatted Output Generate करके Screen पर Display करता है। अन्तर केवल इतना है कि printf() Function Variable Number of Arguments Accept करता है जबकि vprintf() Function दूसरे Argument के रूप में Parameters का एक Array Accept करता है। इसे हम printf() Function की तरह ही निम्नानुसार Use कर सकते हैं:

<?php
	$day = 10;
	$month = 2;
	$year = 2000; 
	
	vprintf("Birth Date: %02d/%02d/%04d", array($month, $day, $year)); 
?>

//Output
   Birth Date: 02/10/2000

sscanf() Function

ये Function printf() Function की तरह ही काम करता है। लेकिन ये Function Output को Directly Return नहीं करता बल्कि Specified Format के Output को एक String Type के Variable में Store करता है और इस Variable को हम हमारी जरूरत के अनुसार कहीं भी Use कर सकते हैं। इस Function का Syntax निम्नानुसार होता हैः

mixed sscanf(string $str, string $format, mixed &$, …)

इस Function में पहले Parameter के रूप में हमें उस Variable को Specify करना होता है, जिससे हम Input के रूप में उस String को प्राप्त करना चाहते है, जिसे इस Function द्वारा Formatted Output के रूप में Generate किया जाना है।

यानी हम जिस String को Format करना चाहते हैं, वह String हमें इस Function के पहले Parameter के रूप में Specify करना होता है। जबकि String को किस तरह से Format करना है, इस बात की Information हम दूसरे Parameter को Specify करते हैं।

इस Function के तीसरे Parameter के रूप में यदि हम किसी Variable को Specify नहीं करते, तो ये Function पहले Argument के रूप में Specified नाम की String के आधार पर Formatted Strings का एक Array Return करता है।

जबकि यदि हम तीसरे Parameter के रूप में विभिन्न Variables को Specify करते हैं, तो ये Function पहले Parameter में Specified String के आधार पर Formatted String Return करता है। इस Function को हम निम्न उदाहरण द्वारा बेहतर तरीके से समझ सकते हैं:

<?php
	$title = "Registration";
	include_once('header.php');

	list($serial) = sscanf("SN/2350001", "SN/%d");

	$mandate = "January 01 2000";
	list($month, $day, $year) = sscanf($mandate, "%s %d %d");

	echo "Item $serial was manufactured on: $year-" . substr($month, 0, 3) . "-$day\n";
	
	include_once('footer.php'); 
?>

इस Code में sscanf() Function में पहले Parameter के रूप में एक String को Specify किया गया है। इसी String को हम Format करना चाहते हैं। इस पहले Argument की String को Format करने के लिए हमने दूसरे Parameter के रूप में “SN/” को ज्यों का त्यों रखा है लेकिन पहले Parameter की Numerical Value के लिए दूसरे Parameter में हमने “%d” Format Specifier का प्रयोग किया है।

परिणामस्वरूप ये Function पहले Parameter के रूप में Specified String को Format करके एक Array Return करता है। चूंकि sscanf() Function में हमने केवल एक ही Format Specifier का प्रयोग किया है, इसलिए ये Function एक ऐसा Array Return करता है, जिसमें केवल एक ही Column होता है और उस Column में पहले Argument की Numerical Value Store रहती है।

इस Numerical Value को list() Function का प्रयोग करके $serial नाम के Variable में Store कर लिया जाता है। फिर हमने निम्नानुसार Code द्वारा एक Date String को $mandate Variable में Store किया है और इस Date को Format करने के लिए फिर से sscanf() Function को Use किया हैः

      $mandate = "January 01 2000";
      list($month, $day, $year) = sscanf($mandate, "%s %d %d");

हम देख सकते हैं कि हमारी Data के तीन हिस्से हैं और तीनों ही हिस्से एक Blank Space से Separated हैं। इसलिए sscanf() Function में जब हम इस Date को Format करते हैं, तो Date के अलग-अलग हिस्सों को अलग-अलग रखने के लिए हम फिर से Format Specifier में भी तीनों के बीच एक Blank Space रखते हैं। यानी हम sscanf() Function में निम्नानुसार Format Specifier का प्रयोग करते हैं:

sscanf($mandate, “%s %d %d”);

इस प्रकार से $mandate नाम के Date Variable में Stored Date के तीनों हिस्सों को हम sscanf() Function द्वारा तीन अलग हिस्सों में Divide कर देते हैं। जब ये Function Run होता है, तब इस Function का तीसरा Parameter Specify न करने की वजह से ये Function एक Array Return करता है, जिसमें तीन Columns होते हैं और ये तीनों Columns के Data क्रमश:  list() Function के $month, $day$year नाम के Variables में Store हो जाते हैं, जिन्हें आगे के Program में जरूरत के अनुसार Use किया जाता है।

यदि हम चाहें, तो sscanf() Function से Array Return करवाने के स्थान पर सीधे ही Formatted String की Values को तीन Variables में Store कर सकते हैं, जहां हमें फिर से list() Function को Use करने की जरूरत ही नहीं रह जाती। इस काम को करने के लिए हम उपरोक्त Code को ही निम्नानुसार भी लिख सकते हैं:

$mandate = "January 01 2000";
sscanf($mandate, "%s %d %d", $month, $day, $year);

जब हम sscanf() Function को इस प्रकार से Use करते हैं, तब ये Function $mandate या अपने पहले Parameter में Specified String को दूसरे Parameter में Specified Format Specifier के अनुसार Format करता है और Results को तीसरे Parameters के रूप में Specified Variables में उसी क्रम में Store कर देता है, जिस क्रम में Format Specifier में Control Strings का प्रयोग किया गया है।

PHP in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook PHP in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

PHP in Hindi | Page: 647 | Format: PDF

BUY NOW GET DEMO REVIEWS