Common PHP 10 string processing instance code

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. determining the length of a string is the most obvious example in this article. The question here is how to determine the length of a string. What we cannot but mention here is the strlen () function: $ textsunnyday; $ countstrlen ($ text); $ coun

Welcome to the Linux community forum and interact with 2 million technical staff> enter 1. determining the length of a string is the most obvious example in this article. The question here is how to determine the length of a string. What we cannot but mention here is the strlen () function: $ text = "sunny day"; $ count = strlen ($ text); // $ coun

Welcome to the Linux community forum and interact with 2 million technicians>

1. determine the length of a string

This is the most obvious example in the article. The question here is how to determine the length of a string. What we cannot but mention here is the strlen () function:

$ Text = "sunny day"; $ count = strlen ($ text); // $ count = 9

2. extract text and create a summary

News websites usually take a section about 200 characters and add a ellipsis at the end of the section to form a summary. In this case, you can use substr_replace () function. Due to the length of the article, only the limit of 40 characters is demonstrated here:

$ Article = "breaking news: In ultimate irony, man bites dog. "; $ summary = substr_replace ($ article ,"... ", 40); // $ summary =" breaking news: In ultimate irony, man bi..."

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

I believe that you will often see blog or news articles to summarize the total number of words in the article, or we often see some requirements for contribution: within a certain word range. In this case, you can use the str_word_count () function to calculate the total number of words in the article:

$ Article = "breaking news: In ultimate irony, man bites dog."; $ wordCount = str_word_count ($ article); // $ wordCount = 8

Sometimes you need to strictly control the space used by contributors, such as comments. If you want to know the number of characters to form an array, use the count_chars () function.

4. parse CSV files

Data is usually stored in a file (such as a known CSV file) in a comma separated form. The CSV file uses a comma or is similar to a predefined symbol, separate each column of strings into a single row. You may often create PHP scripts to import the data or parse what you need. Over the years, I have seen many methods to parse CSV files, the most common is to use a combination of fgets () and explode () functions to read and Parse Files. However, the simplest way is to use a function to solve the problem, but it is not part of the PHP string processing Library: fgetcsv () function. Using the fopen () and fgetcsv () functions, we can easily parse this file and retrieve the name of each contact at the same time:

$ Fh = fopen ("contacts.csv", "r"); while ($ line = fgetcsv ($ fh, 1000, ",") {echo "Contact: {$ line [1]} ";}

5. convert to a String Array

In some cases, you may need to create CSV files and read them at the same time, which means you need to convert the strings separated by commas into data. If the data is first retrieved from the database, it may only provide you with an array. In this case, you can use the implode () function to convert these strings into an array:

$ Csv = implode (",", $ record );

6. Convert the URL to a hyperlink

Currently, many WYSIWYG editors provide toolbar that allow users to mark text, including hyperlinks. However, when the content is displayed on the page, you can easily perform this process automatically without any additional errors. To convert a URL to a hyperlink, you can use the preg_replace () function to search for a string based on a regular expression and define the URL structure:

$ Url = "W. j. gilmore, LLC (http://www.xker.com) "; $ url = preg_replace ("/http: // ([A-z0-9. /-] +)/"," $0 ", $ url); // $ url =" W. j. gilmore, LLC )"

[1] [2]

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.