ටික කාලෙකට පස්සේ අයෙත් වැදගත් දෙයක් ගැන ලියන්න හිතුනා.මේ ඇත්තටම ටිකක් විතර පස්සේ වැදගත් වෙන්න පුළුවන් දෙයක්, විශේෂයෙන්ම, Database එකකට Forms වලින් දත්ත ලබාදෙන වෙලාවල අපි සිදුකරන Form Validation වගේ දේවලට මේව භාවිතා කරන වෙලාවල් තියනවා.
මේවා කියන්නේ ඇත්තටම String Manipulations. හොදයි අපි බලමු අපිට හමුවන String Manipulations මොනවාද කියල.
පහත උදාහරණය බලන්න. එහි String Manipulations සදහා භාවිතා වන functions කියපක්ම තියනවා. හැම එකක්ම වගේ str ලෙස ආරම්භ කර තිබීම ඔබට පහසුවක් වේවි IDE එකක් භාවිත කර PHP development කරනවානම්.
<?php $text = "Harsha Wansooriya"; $string = str_repeat($text,5); echo "The Original Text is ".$text." "; echo "Manipulated Text is ".$string; ?>
str_repeat() function එක මගින් සිදුවන්නේ ලබාදෙන වාරගණනක් එකම text එක output එකේ display කිරීමයි. මෙහිදී parameters ලෙස string එකක් සහ integer අගයක් ලබාදිය යුතු වනවා
<?php
$text = "Hello World";
$string = str_replace("o","b",$text);
echo "Original Text is ".$text." ";
echo "Manipulated Text is ".$string;
?>
str_replace() function එක මගින් සිදු වන්නේ කිසියම් text එකක් හෝ letter එකක් තවත් එවැනිම එකකින් replace කිරීමයි. Parameters වලින් පළමුවෙන්ම replace කල යුතු value එකත්, දෙවනුව ඒ සදහා භාවිතා කරන text value එකත දැක්වෙනවා.
<?php $text = "Hello World"; $string = strchr($text,"ello"); echo $string.""; // output: ello World $string = stristr($text,"ELLO"); echo $string.""; // output: ello World $string = strlen($text); echo $string.""; // output: 11 $string = strpos($text,"W"); echo $string.""; // output: 6 $string = strrev($text); echo $string.""; // output: dlroW olleH $string = strtoupper($text); echo $string.""; // output: HELLO WORLD $string = substr_count($text,"l"); echo $string; // output: 3 ?>
ඉහත උදාහරණ තුලින් ඉතා වැදගත් වෙයියයි සිතු functions කියපක් එක් කර තියනවා. මෙහි output එක පහත ආකාරයෙන් වනවා අවබෝධ කරගන්න උත්සහ කරන්න.
ello World ello World 11 6 dlroW olleH HELLO WORLD 3
මෙහි strchr() සහ stristr() දෙකේ වෙනස වන්නේ, strchr() වල අප දෙවන Parameter එක ලෙස ලබා දෙන text එක case sensitive වීමයි. නමුත්, stristr() හි එය case sensitive වීමක් වන්නේ නැ.
බලන්න, $text variable එකේ store කර ඇති text එකේ 'ELLO' ලෙස කොටසක් නැති වුනත් (Upper case / Lower Case සැලකු විට), එය strchr() ලබා දෙන output එකම ලබා දී ඇති අකාරය.
ස්තුතියි රැදී සිටියාට !

Comments
Post a Comment