PHP substr_count() ফাংশন
উদাহরণ
"Shanghai" শব্দচিহ্নের শব্দচিহ্নের মধ্যে কতবার উপস্থিত হয় তা গণনা করুন:
<?php echo substr_count("I love Shanghai. Shanghai is the biggest city in china.","Shanghai"); ?>
substr_count() ফাংশন উপশব্দটি শব্দচিহ্নের মধ্যে কতবার উপস্থিত হয় তা গণনা করে。
মন্তব্য:উপশব্দ হারফ ভিন্নতা সহযোগে পরিগণিত হয়。
মন্তব্য:এই ফাংশন অবরোধিত উপশব্দগুলির সংখ্যা গণনা করে না (দেখুন উদাহরণ 2)。
মন্তব্য:if start পারামিটার সহ length পারামিটার শব্দচিহ্ন শব্দচিহ্নের দৈর্ঘ্য বেশি হলে, এই ফাংশন একটি সতর্কতা উৎপন্ন করবে (দেখুন উদাহরণ 3)。
সংজ্ঞা
substr_count(string,substring,start,length)
পারামিটার | বর্ণনা |
---|---|
string | অপশনাল।পরীক্ষা করা হলের শব্দচিহ্ন নির্ধারণ করুন。 |
substring | অপশনাল।সার্চ করতে হলের শব্দচিহ্ন নির্ধারণ করুন。 |
start | অপশনাল।শব্দচিহ্নের কোথাথে সার্চ শুরু করবে তা নির্ধারণ করুন。 |
length | অপশনাল।সার্চের দৈর্ঘ্য নির্ধারণ করুন。 |
Technical Details
Return Value: | Returns the number of times the substring appears in the string. |
PHP Version: | 4+ |
Update Log: | In PHP 5.1, a new start and length Parameters. |
More Examples
Example 1
Use all parameters:
<?php $str = "This is nice"; echo strlen($str)."<br>"; // Use strlen() to return the length of the string echo substr_count($str,"is")."<br>"; // The number of times "is" appears in the string echo substr_count($str,"is",2)."<br>"; // The string is reduced to "is is nice" echo substr_count($str,"is",3)."<br>"; // The string is reduced to "s is nice" echo substr_count($str,"is",3,3)."<br>"; // The string is reduced to "s i" ?>
Example 2
Overlapping Substring:
<?php $str = "abcabcab"; echo substr_count($str,"abcab"); // This function does not count overlapping substrings ?>
Example 3
if start and length If the parameters exceed the length of the string, the function will output a warning:
<?php echo $str = "This is nice"; substr_count($str,"is",3,9); ?>
Because the length value exceeds the length of the string (3 + 9 is greater than 12), a warning will be output when used.