10 common string handling cases in PHP

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags allow users array create csv data echo example examples

PHP's ability to handle strings is powerful and varied, but sometimes you need to choose the simplest and most ideal solution. This article lists 10 common examples of string processing in PHP and provides the best way to handle them.

1. Determine the length of a string

This is one of the most obvious examples of the article, the question is how we can determine the length of a string, and here we have to mention the strlen () function:

$text = "Sunny Day"; $count = strlen ($text); $count = 9

2. Intercept text, create a summary

A news site usually intercepts a paragraph about 200 words and appends an ellipsis to the end of the second paragraph to form a summary, at which point you can use the Substr_replace () function to implement this function. For space reasons, only the 40-character limit is shown here:

$article = "Breaking news:in ultimate irony, man bites dog.";

$summary = Substr_replace ($article, "...", 40);

$summary = "Breaking news:in ultimate irony, man bi ..."

3. Count characters and words in a string

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

$article = "Breaking news:in ultimate irony, man bites dog.";

$wordCount = Str_word_count ($article); $wordCount = 8

Sometimes you need more stringent control over the contributors ' space, 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. csv file resolution

Data is usually stored in a comma-delimited form in a file (such as a known CSV file), and a CSV file uses a comma or similar to a predefined symbol to make each column string a separate row. You can often create PHP scripts to import this data, or parse out what you need, and over the years, I've seen many ways to parse CSV files, most commonly using a combination of fgets () and explode () functions to read and parse files, but 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:

$fh = fopen ("Contacts.csv", "R");

while ($line = Fgetcsv ($fh, 1000, ","))

{echo ' contact: {$line [1]} ';}

5. Convert to an array of strings

Sometimes you might want to create a CSV file and read it in these files, which means you need to convert those strings separated by commas 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 to an array:

$csv = Implode (",", $record);

6. Convert URLs to hyperlinks

Many WYSIWYG editors currently offer toolbars that allow users to tag text, including hyperlinks. However, when content is rendered on the page, you can easily automate this process while ensuring that you do not have additional errors. To convert to a hyperlink URL, you can use the Preg_replace () function, which searches for a string according to the regular expression and defines the structure of the URL:

$url = "W.J Gilmore, LLC (http://www.jzread.com)"; http://www.jzread.com) "

$url = Preg_replace ("/http://([a-z0-9./-]+)/", "$", $url);

$url = "W.J Gilmore, LLC (

7. Remove 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, and if so, this can lead to SQL injection or script attacks. The PHP language contains a number of security features that can help you filter data, including extending the filter. For example, you can allow users to have some basic HTML statements, including some comments. To implement this function, you can use a 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 the label you specify. For example, in the following example, you can remove all tags:

$text = Strip_tags ($input, "

");

8. Compare two strings

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

$PSWD = "secret";

$PSWD 2 = "secret";

if (! strcmp ($PSWD, $pswd 2))

{echo "The passwords are not identical!";

}

If you want to judge that two strings are case-insensitive, you can use the strcasecmp () function.

9. Convert line break

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

$comment = NL2BR ($comment);

10. Apply Automatic Wrapping

To apply word wrapping, you can use this function in PHP: WordWrap ():

$speech = "Loko score and seven Perton ago we fathers brought forth, upon this continent, a new nation, conceived in Liberty, and de Dicated to the proposition so all men are created equal. ";

Echo WordWrap ($speech, 30);

Execute the above code, and the result is:

Loko score and seven Perton ago we fathers brought, forth this upon, a new continent, nation in conceived, and Liberty to The proposition that all men are created equal.

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.