10 simple ways to handle PHP strings

  • 2020-03-31 20:51:38
  • OfStack

Determine the length of a string

This is the most obvious example in the article. The problem is how to determine the length of a string.
 
$text = "sunny day"; 
$count = strlen($text); // $count = 9 

2. Capture the text to create a summary

You can use the substr_replace() function to capture a 200-word paragraph and add an ellipsis at the end of the paragraph to form a summary. For reasons of space, 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..." 

Count the number of characters and words in the string

I believe you will often see some blogs or news articles, to summarize the total number of words of the article, or we often see some submission requirements: within a certain number of words. At this point, you can use the str_word_count() function to calculate the total number of words in an article:
 
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; 
$wordCount = str_word_count($article); // $wordCount = 8 

Sometimes you need to control the space of the contributors more strictly, such as some comments and so on. If you want to know how many characters make up an array, use the count_chars() function.

4. Parse CSV files

The data is usually stored in a comma-separated file (such as a known CSV file), which USES a comma or similar predefined notation to form a separate row for each column string. You can often create a PHP script to import these data, or parsed out what you need, over the years, I have seen a lot of parsing CSV file method, one of the most common is to use the fgets () and explodes the combination of () function to read and parse the file, however, the simplest method is to use a function to solve the problem, but it does not belong to part of the PHP string handling Treasury: 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

At some point, you might want to create and read CSV files, which means you need to convert comma-separated strings into data. If the data was originally retrieved from the database, it will probably give you just one array. At this point, you can use the implode() function to convert these strings into an array:
 
$csv = implode(",", $record); 

6. Convert urls to hyperlinks

Many WYSIWYG editors currently provide toolbars that allow users to tag text, including hyperlinks. However, when the content is rendered to the page, you can easily automate this process without making additional errors. To convert the URL to a hyperlink, you can use the preg_replace() function, which searches for a string in terms of a regular expression and defines the structure of the URL:
 
$url = "W.J. Gilmore, LLC (http://www.wjgilmore.com)"; 
$url = preg_replace("/http://([A-z0-9./-]+)/", "$0", $url); 
// $url = "W.J. Gilmore, LLC (http://www.wjgilmore.com)" 

Remove HTML tags from a string

One of the main jobs of a Web developer is to make sure that user input does not contain dangerous characters, which can lead to SQL injection or scripting attacks. The PHP language includes a number of security features that help you filter data, including extended filters. For example, you can allow the user to include some basic HTML statements, including some comments. To do this, you can use the strip_tags() function with check. It removes all HTML tags from the string by default, but also allows you to override the default or the tags you specify. For example, in the following example, you can remove all tags:
 
$text = strip_tags($input, " "); 

Compare two strings

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

 
$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. Converts newline characters

In this article, I've shown you how to easily convert urls to hyperlinks, and now I'm going to introduce the nl2br() function, which helps you convert any line breaks into HTML tags.
 
$comment = nl2br($comment); 

10. Apply line wrap

With 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 were created equal.

The original address: http://phpbuilder.com/columns/Jason_Gilmore060210.php3
10 Easy Solutions for PHP String Manipulation
By w. Jason Gilmore

Related articles: