1. Determine the length of a string
This is the most obvious example in the article. The problem is how we determine the length of a string. What we cannot ignore here is the strlen() function:
$text = "sunny day";
$count = strlen($text); // $count = 9
2. Snap the text and create a summary
News-based websites usually intercept a paragraph of about 200 words and add an ellipsis to the end of the subparagraph to form a summary. At this time, you can use the substr_replace() function to implement this function. Due to space reasons, here is only a 40-character limit:
$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 you will often see some blogs or news articles to summarize the total number of words in the article, or we also often see some submission requirements: within a certain word count range. At this time, you can use the str_word_count() function to calculate the sum of the 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 use space of contributors, such as some annotations, etc. If you want to know how many characters there are to form an array, use the count_chars() function.
4. Parsing CSV files
Data is usually stored in a file in a comma-separated form (such as a known CSV file), which uses a comma or similar to a predefined symbol to form each column of strings into a separate line. You may often create PHP scripts to import this data or parse out what you need. Over the years, I have also seen many ways to parse CSV files. The most common one is to use a combination of fgets() and exploit() functions to read and parse files. However, the easiest way is to use a function to solve the problem, but it does not belong to the part of PHP's string processing library: the fgetcsv() function. Using fopen() and fgetcsv() functions, we can easily parse this file and retrieve the name of each contact:
$fh = fopen("", "r");
while($line = fgetcsv($fh, 1000, ","))
{ echo "Contact: {$line[1]}"; }
5. Convert to an array of strings
Sometimes, you may need to create CSV files and read them in them, which means you need to convert comma-separated strings into data. If this data was initially retrieved from the database, it will most likely give you only an array. At this point, you can use the implode() function to convert these strings into an array:
$csv = implode(",", $record);
6. Convert URL to hyperlink
Many toolbars currently provided by WYSIWYG editors allow users to tag text, including hyperlinks. However, when the content is rendered onto the page, you can easily automate this process while ensuring that you do not experience additional errors. To convert to a hyperlink URL, you can use the preg_replace() function, which can search for a string according to the regular expression and define the structure of the URL:
$url = ". Gilmore, LLC ()";
$url = preg_replace("/http://([A-z0-9./-]+)/", "$0", $url);
// $url = ". Gilmore, LLC ()"
7. Remove HTML tags from a string
As a web developer, one of the main tasks is to make sure that the user input does not contain dangerous characters, if any, which can lead to SQL injection or script attacks. PHP language contains many security features that can help you filter data, including extended filters. For example, you can allow users to have some basic HTML statements, including some comments. To implement this function, you can use the function with the check function: strip_tags(). By default, it removes all HTML tags from the string, but also allows overriding the default or tags you specify. For example, in the following example, you can remove all tags:
$text = strip_tags($input, " ");
8. Comparison of two strings
Compare the two strings to make sure they are the same. For example, to determine whether the password entered by the user is the same as the second time, you can use the substr_compare() function to easily make a reality:
$pswd = "secret";
$pswd2 = "secret";
if (! strcmp($pswd, $pswd2))
{ echo "The passwords are not identical!"; }
If you want to determine that two strings are case-insensitive, you can use the strcasecmp() function.
9. Convert line breaks
In this article, I introduced how to easily convert to a hyperlink URL. Now I introduce the nl2br() function, which can help you convert any newline character to HTML tags.
$comment = nl2br($comment);
10. Apply automatic line wrap
To apply automatic line wrap, you can use this function in PHP: wordwrap():
$speech = "Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.";
echo wordwrap($speech, 30);
Execute the above code and the result is:
Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
Original address: /columns/Jason_Gilmore060210.php3
Original name: 10 Easy Solutions for PHP String Manipulation
Author: W. Jason Gilmore
This is the most obvious example in the article. The problem is how we determine the length of a string. What we cannot ignore here is the strlen() function:
Copy the codeThe code is as follows:
$text = "sunny day";
$count = strlen($text); // $count = 9
2. Snap the text and create a summary
News-based websites usually intercept a paragraph of about 200 words and add an ellipsis to the end of the subparagraph to form a summary. At this time, you can use the substr_replace() function to implement this function. Due to space reasons, here is only a 40-character limit:
Copy the codeThe code is as follows:
$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 you will often see some blogs or news articles to summarize the total number of words in the article, or we also often see some submission requirements: within a certain word count range. At this time, you can use the str_word_count() function to calculate the sum of the number of words in the article:
Copy the codeThe code is as follows:
$article = "BREAKING NEWS: In ultimate irony, man bites dog.";
$wordCount = str_word_count($article); // $wordCount = 8
Sometimes you need to strictly control the use space of contributors, such as some annotations, etc. If you want to know how many characters there are to form an array, use the count_chars() function.
4. Parsing CSV files
Data is usually stored in a file in a comma-separated form (such as a known CSV file), which uses a comma or similar to a predefined symbol to form each column of strings into a separate line. You may often create PHP scripts to import this data or parse out what you need. Over the years, I have also seen many ways to parse CSV files. The most common one is to use a combination of fgets() and exploit() functions to read and parse files. However, the easiest way is to use a function to solve the problem, but it does not belong to the part of PHP's string processing library: the fgetcsv() function. Using fopen() and fgetcsv() functions, we can easily parse this file and retrieve the name of each contact:
Copy the codeThe code is as follows:
$fh = fopen("", "r");
while($line = fgetcsv($fh, 1000, ","))
{ echo "Contact: {$line[1]}"; }
5. Convert to an array of strings
Sometimes, you may need to create CSV files and read them in them, which means you need to convert comma-separated strings into data. If this data was initially retrieved from the database, it will most likely give you only an array. At this point, you can use the implode() function to convert these strings into an array:
Copy the codeThe code is as follows:
$csv = implode(",", $record);
6. Convert URL to hyperlink
Many toolbars currently provided by WYSIWYG editors allow users to tag text, including hyperlinks. However, when the content is rendered onto the page, you can easily automate this process while ensuring that you do not experience additional errors. To convert to a hyperlink URL, you can use the preg_replace() function, which can search for a string according to the regular expression and define the structure of the URL:
Copy the codeThe code is as follows:
$url = ". Gilmore, LLC ()";
$url = preg_replace("/http://([A-z0-9./-]+)/", "$0", $url);
// $url = ". Gilmore, LLC ()"
7. Remove HTML tags from a string
As a web developer, one of the main tasks is to make sure that the user input does not contain dangerous characters, if any, which can lead to SQL injection or script attacks. PHP language contains many security features that can help you filter data, including extended filters. For example, you can allow users to have some basic HTML statements, including some comments. To implement this function, you can use the function with the check function: strip_tags(). By default, it removes all HTML tags from the string, but also allows overriding the default or tags you specify. For example, in the following example, you can remove all tags:
Copy the codeThe code is as follows:
$text = strip_tags($input, " ");
8. Comparison of two strings
Compare the two strings to make sure they are the same. For example, to determine whether the password entered by the user is the same as the second time, you can use the substr_compare() function to easily make a reality:
Copy the codeThe code is as follows:
$pswd = "secret";
$pswd2 = "secret";
if (! strcmp($pswd, $pswd2))
{ echo "The passwords are not identical!"; }
If you want to determine that two strings are case-insensitive, you can use the strcasecmp() function.
9. Convert line breaks
In this article, I introduced how to easily convert to a hyperlink URL. Now I introduce the nl2br() function, which can help you convert any newline character to HTML tags.
Copy the codeThe code is as follows:
$comment = nl2br($comment);
10. Apply automatic line wrap
To apply automatic line wrap, you can use this function in PHP: wordwrap():
Copy the codeThe code is as follows:
$speech = "Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.";
echo wordwrap($speech, 30);
Execute the above code and the result is:
Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
Original address: /columns/Jason_Gilmore060210.php3
Original name: 10 Easy Solutions for PHP String Manipulation
Author: W. Jason Gilmore