What are the common functions of strings in PHP? What are the common operations of PHP strings? Let's take a look at it in detail next.
1. Determine the length of a string
This is the most common and basic example, for determining the length of a string, we should use the strlen () function, for example to get the length of the following string $text:
$text = "Sunnyday"; $count =strlen ($text);//$count =9
2. Truncate text to create a profile
News-Nature sites typically intercept about 200 words at the beginning of the release text, and add ellipses at the end to form a summary. You can use the Substr_replace () function to implement this function, and here is an example of a 40-character intercept for space reasons:
$article = "Breakingnews:inultimateirony,manbitesdog."; $summary =substr_replace ($article, "...", 40); $summary = "Breakingnews:inultimateirony,manbi ..."
3. Calculate the number of words in a string
You can often see some blogs or news articles that will count the total number of words in the article, or the articles that require the user to contribute, within a certain range of words. For this, you can use the Str_word_count () function to count the number of words, such as:
$article = "Breakingnews:inultimateirony,manbitesdog."; $wordCount =str_word_count ($article); $wordCount =8
4. Parse the CSV file
Data is usually stored in a comma-delimited format (CSV), which 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 parse out what you need, there are many ways to parse a CSV file, most commonly using a combination of fgets () and explode () functions to read and parse files. But the simplest way is to use the Fgetcsv () function, for example, now there is a CSV file:
1,john,smith,plumber2,mark,seagal,instructor3,peter,haines,writer
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, +, ",")) {echo "contact:{$line [1]}"; }
5. Converting an array to a string
There are times when you want to read from these files after you create a CSV file, which means you need to convert the data to a comma-delimited string. If the data was originally retrieved from the database, it is likely to provide an array. At this point, you can use the implode () function to combine array elements into a single string. As shown below:
$csv =implode (",", $record);
6. Convert a URL to a hyperlink
Many of the WYSIWYG (see-what-you-get) editor tools now allow users to tag text, including hyperlinks. Of course, you can also automate this process when content is rendered to a page. To convert to hyperlinks, you can use the Preg_replace () function, which searches for a string by 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./-]+)/", "$ A", $url); $url = "W.J.GILMORE,LLC (http://www.wjgilmore.com)"
7. Removing HTML tags from a string
We usually need to make sure that the user's input does not contain dangerous characters because they can cause SQL injection or cross-site scripting attacks. PHP has a lot of security considerations and features that can help you filter your data. Your site might allow users to have some basic HTML elements in their comments, such as some tags. At this point, you need to use the function with the check function: Strip_tags (), you can remove all HTML tags from the string. For example:
$text =strip_tags ($input, "");
8. Comparison of two strings
Compare two strings to check if they are identical. For example, determine whether the first time a user sets a password and the second time the password is entered exactly the same. At this point, you can use the strcmp () function to implement:
$PSWD = "secret"; $PSWD 2= "Secret"; if (!strcmp ($PSWD, $pswd 2)) {echo "thepasswordsarenotidentical!";}
9. Converting line breaks to newline labels
Use the NL2BR () function to help you convert any newline character into an HTML tag, such as:
$comment =nl2br ($comment);
10. Using Line Wrapping
To apply word wrap, you can use this function in PHP: WordWrap () to wrap a string by a specified length. Like what:
$speech = "Fourscoreandsevenyearsagoou***thersbroughtforth, Uponthiscontinent,anewnation,conceivedinliberty, Anddedicatedtothepropositionthatallmenarecreatedequal. "; Echowordwrap ($speech, 30);//parameter width:30 to the specified maximum line width