Experience sharing: A simple way to handle PHP strings _php Tutorial

Source: Internet
Author: User
Tags php language vars word wrap
PHPThe ability to handle strings is very powerful, and there are many ways to do it, but sometimes you need to choose the simplest and most desirable solution. This article lists 10 common string processing cases in PHP, and provides the most appropriate processing methods.

1. Determine the length of a string

This is the most obvious example of the article, where the question is how we can determine the length of a string, and here we cannot fail to mention the strlen () function:

 
  
  
  1. $text"Sunny Day"$countstrlen($ Text//$count = 9

2. Truncate the text and create a summary

News-Nature sites usually intercept a paragraph about 200 words, and add ellipses at the end of the paragraph to form a summary, and you can use the Substr_replace () function to implement this function. For space reasons, only the 40-character limit is shown here:

 
  
  
  1. $article"Breaking news:in ultimate irony, man bites dog." ;
  2. $summary = Substr_replace ($article"...", +);
  3. $summary = "Breaking news:in ultimate irony, man bi ..."

3. Calculating the number of characters and words in a string

I believe you will often see some blog or news articles, to summarize the total number of words, or we often see some of the requirements of the submission: within a certain range of words. At this point, you can use the Str_word_count () function to calculate the sum of the words in the article:

 
  
  
  1. $article"Breaking news:in ultimate irony, man bites dog." ;
  2. $wordCountstr_word_count($article);
  3. $wordCount = 8

There are times when you need to be more tightly controlled by contributors, such as some annotations and so on. If you want to know how many characters make up an array, use the Count_chars () function.

4. Parse the CSV file

The data is usually stored in a comma-delimited file (such as a known CSV file), and the CSV file uses a comma or similar to a predefined symbol to make each column string a separate line. You may often create PHP scripts to import this data, or to parse out what you need, over the years, I have seen many methods of parsing CSV files, most commonly using the combination of fgets () and explode () functions to read and parse files, however, The simplest approach is to use a function to solve the problem, but it is not part of the PHP string processing library: the Fgetcsv () function. Using the fopen () and Fgetcsv () functions, we can easily parse the file and retrieve the name of each contact:

 
  
  
  1. $FHfopen("Contacts.csv""r");
  2. while ($linefgetcsv($fh"," echo"Contact: {$line [1]}"

5. Converting to a string array

At some point, you may need to create a CSV file and read it in these files, which means you need to convert the comma-delimited strings into data. If the data was originally retrieved from the database, it is likely that it will only provide you with an array. At this point, you can use the implode () function to convert these strings into an array

 
  
  
  1. $csv = implode (","$record

6. Convert URLs to hyperlinks

Currently, many WYSIWYG editors provide toolbars that allow users to tag text, including hyperlinks. However, when content is rendered to a page, you can easily automate the process and ensure that you do not get any additional errors. To convert a URL to a hyperlink, you can use the Preg_replace () function, which searches for a string by regular expression and defines the structure of the URL:

 
  
  
  1. $url"W.J Gilmore, LLC (http://www.wjgilmore.com)";
  2. $url = preg_replace ("/http://([a-z0-9./-]+)/""$$url"
  3. $url"W.J Gilmore, LLC (http://www.wjgilmore.com)"

7. Removing HTML tags from a string

As a web developer, one of the main tasks is to ensure that user input does not contain dangerous characters, which, if any, can result in SQL injection or scripting attacks. The PHP language contains a number of security features that can help you filter data, including extending filters. For example, you can allow the user to have some basic HTML statements, including some comments. To implement this function, you can use the function with check function: Strip_tags (). It removes all HTML tags from the string by default, but it also allows you to override the default or tags you specify. For example, in the following example, you can remove all the tags:

 
  
  
  1. $textstrip_tags($input""

8. Comparison of two strings

Compare two strings to make sure they are the same. For example, you can use the Substr_compare () function to make it easy to determine if the user first entered the same password as the second time:

 
  
  
  1. $PSWD"secret"$pswd 2"secret";
  2. Ifstrcmp($pswd$pswd 2 echo"The passwords is not identical!"

If you want to determine that two strings are not case-sensitive, you can use the strcasecmp () function.

9. Convert newline characters

In this article I describe how to easily convert URLs to hyperlinks, and now introduce the NL2BR () function, which can help you convert any newline character into an HTML tag.

 
  
  
  1. $commentnl2br($comment

10. Apply the Wrap line

Apply word wrap, you can use this function in PHP: WordWrap ():

 
  
  
  1. $speech and seven years ago our fathers brought forth,
  2. New Nation, conceived in Liberty,
  3. and dedicated to the proposition, all men is created equal. ";
  4. Echo wordwrap ($speech

Execute the above code, and the result is:

 
  
  

Original Address: http://phpbuilder.com/columns/Jason_Gilmore060210.php3


http://www.bkjia.com/PHPjc/445747.html www.bkjia.com true http://www.bkjia.com/PHPjc/445747.html techarticle PHP's ability to handle strings is very powerful, and there are a variety of ways, but sometimes you need to choose the simplest and most desirable solution. The article lists 10 common characters in PHP ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.