Đang chuẩn bị liên kết để tải về tài liệu:
PHP 5 Recipes A Problem-Solution Approach 2005 phần 5

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Tuy nhiên, mô hình hướng đối tượng tồn dư từ PHP 3, thì hầu không được chú ý đến. Mặc dù mô hình đối tượng này cực kì hạn chế song chúng vẫn được sử dụng hết sức rộng rãi trên thế giới thậm chí là ngay cả trong các ứng dụng PHP rất lớn. Nhu cầu sử dụng rất cao đối với mô hình lập trình kém của nó đã khiến cho các nhà kiến tạo ngôn ngữ quyết định chọn nó làm mục tiêu chính để nâng cấp PHP 4 lên PHP 5 | 270 6-4 USING SUBSTRING ALTERNATIVES height of the block where the text is supposed to be output is big enough to handle only 300 characters of text. If the amount of text outputted exceeds that the site could potentially break causing the entire design to look flawed this is not good considering this is an important client. In addition consider that the client is not sure at all how many characters to use and even if you had informed them to use only 300 there is still a real chance they would try their luck anyway. How then can you guarantee that the design will not break Well this sounds like a lovely test for our good friend Mr. substr and his buddy Ms. strlen . The Code php theclientstext Hello how are you today I am fine if strlen theclientstext 30 echo substr theclientstext 0 29 else echo theclientstext Hello how are you today I a How It Works The first thing this block of code does is check to make sure the text provided by the client is within the length you need it to be if strlen theclientstext 30 If it happens to fall outside the range of acceptable length you then use the lovely substr function to echo only the portion of the text that is deemed acceptable. If the client has entered a proper block of text then the system merely outputs the text that was entered and no one is the wiser. By using the function substr you have averted a potential disaster. People browsing the site will see nothing but a slightly concatenated set of verbiage so the site s integrity remains sound. This sort of rock-solid validation and programming can save business relationships as clients are seldom fond of having their site appear broken to potential customers or intrigued individuals. 6-4. Using Substring Alternatives You can consider the substr function as something of a jack of all trades. It can get you whatever you are looking for in a string with the greatest of ease. Sometimes however it may not be necessary to go to the trouble of using such a versatile function. .