1 / 13

Strings and Regular Expressions

Learn about string manipulation, regular expressions, and various string functions in PHP. Explore performance considerations and cool string manipulation techniques. Adapted from Mac Newbold's slides.

lasterm
Télécharger la présentation

Strings and Regular Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings and Regular Expressions Adapted from: Mac Newbold slides

  2. Outline • Intro to Strings in PHP • Functions relating to HTML, SQL, etc. • Performance/Speed considerations • Grab bag of cool string functions • Regular Expressions • PCRE • POSIX

  3. Must-Have String Functions • www.php.net/strings • echo/print • چاپ رشته در خروجی • trim, ltrim, rtrim/chop • حذف فواصل سفید ( یا کاراکترهای دلخواه) • $trimmed = rtrim($text, " \t."); • explode, implode/join • $arr = explode(“ “, “List of words”); • $str = implode(“,”,$arr); جدا کننده

  4. Obligatory C-like Functions • printf, sprintf, sscanf, fprintf • strcmp, strlen, • strpos, • $pos = strpos($mystring, $findme); • Strtok • یک رشته را بر اساس کاراکترهای داده شده به چند رشته کوچکتر (نشانه) تبدیل می کند. • $tok = strtok($string, " \n\t"); • نکته: درتوابعی مثل strpos خروجی صفر معتبر است و به معنای false نیست. در این حالتها ، برای چک کردن خروجی از “===false” استفاده کنید.

  5. Basic String Manipulation • str_replace(“bar”,”baz”,”foobar”); • str_repeat(“1234567890”,8); • strtolower, strtoupper • ucfirst, ucwords • اولین کاراکتر جمله، یا اولین کاراکتر هر کلمه را به بزرگ تبدیل کن. • str_pad(“tooshort”,15,” ”); پد سایز نهایی

  6. <?php $text = "The quick brown fox jumped over the lazy dog."; $newtext = wordwrap($text, 20, "<br />\n"); echo $newtext;?> خروجی: The quick brown fox<br /> jumped over the lazy<br /> dog.

  7. Formatting functions • vprintf, vfprintf, vsprintf • خروجی فرمت دار • number_format • دسته بندی هزار تایی • money_format • فرمت مناسب برای پول • date(), strftime() • تبدیل زمان به رشته • strtotime(), strptime() • تبدیل رشته به زمان

  8. URL Functions • url{en, de}code • کاراکترهای غیر نوشتاری را به %[hex] و ‘ ‘ را به ’+’تبدیل می کند. • مورد استفاده این تابع هنگامی است که میخواهیم ازطریق URI مقادیر پارامترها را ارسال کنیم.(یعنی متد GET) • rawurl{en,de}code • بجز در مورد ‘ ’ در بقیه موارد مثل url{en, de}code عمل می کند. • در اینجا ‘ ‘ به %20 تبدیل می شود.

  9. URL Functions • parse_url • URL داده شده را به host, path و query تبدیل می کند. • مثال:http://en-us.start2.mozilla.com/firefox?client=firefox-a&rls=me Host=en-us.start2.mozilla.com, path=firefox, query=client=firefox-a&rls=me • http_build_query • یک آرایه را تبدیل به یک query آرایه ای تبدیل می کند. <?php $data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk');echo http_build_query($data);// foo=bar&baz=boom&cow=milk • base64_{en,de}code • تبدیل base64 که در MIME استفاده می شود.

  10. HTML Functions • htmlspecialchars • کاراکترهای &, “, <, و > را با &amp;, &quot;, &lt;, و &gt; جایگزین می کند. • htmlentities کاراکترهای بیشتری را جایگزین می کند. • html_entity_decode معکوس htmlentities است. • مثال: <?php $new = htmlspecialchars ( "<a href='test'>Test </a>", ENT_QUOTES); echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;?> • nl2br • (\n) را با برچسب <br> عوض می کند. • parse_str • یک query از نوع GET را به متغییر (از نوع آرایه) تجزیه می کند.

  11. strip_tags • برچسبهای HTML را جدا میکند. <?php$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';echo strip_tags($text);echo "\n";// Allow <p> and <a>echo strip_tags($text, '<p><a>');?> Test paragraph. Other text <p>Test paragraph.</p> <a href="#fragment">Other text</a>

  12. Grab Bag • md5, md5_file • با استفاده از md5 رشته یا فایل داده شده را رمز می کند. • برای ذخیره رمز عبور در پایگاه داده مفید است. • levenshtein, similar_text • درجه مشابهت نوشتاری دو رشته را محاسبه می کند. • metaphone, soundex • درجه مشابهت گفتاری دو رشته را محاسبه می کند.

  13. Grab Bag 2 • str_rot13 • یک الگوریتم رمز نگاری • str_shuffle • کاراکترهای کلمه را به هم می ریزد. • count_chars, str_word_count • آماری مفید در مورد رشته • str_rev • معکوس کردن یک رشته

More Related