دالة PHP substr ()
مثال
إرجاع "العالم" من السلسلة:
<?php
echo substr("Hello world",6);
?>
التعريف والاستخدام
ترجع الدالة substr () جزءًا من سلسلة.
بناء الجملة
substr(string,start,length)
قيمه المعامل
Parameter | Description |
---|---|
string | Required. Specifies the string to return a part of |
start | Required. Specifies where to start in the string
|
length | Optional. Specifies the length of the returned string. Default is to the end of the string.
|
تفاصيل تقنية
قيمة الإرجاع: | تُرجع الجزء المُستخرج من سلسلة ، أو FALSE عند الفشل ، أو سلسلة فارغة |
---|---|
إصدار PHP: | 4+ |
التغيير: | PHP 7.0 - إذا كانت السلسلة = البداية (بطول الأحرف) ، فستُرجع سلسلة فارغة. الإصدارات السابقة ترجع FALSE. PHP 5.2.2 - 5.2.6 - إذا كان للبداية موضع اقتطاع سلبي ، يتم إرجاع FALSE. الإصدارات الأخرى تحصل على السلسلة من البداية. |
مزيد من الأمثلة
مثال
استخدام معلمة البداية بأرقام موجبة وسالبة مختلفة:
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
مثال
استخدام معلمات البدء والطول بأرقام موجبة وسالبة مختلفة:
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>
❮ مرجع سلسلة PHP