[Happy to learn PhP100 days] day 5: get started with basic skills (on)

Source: Internet
Author: User
Current motto:

More advanced technologies require solid basic skills. The primary condition for learning basic skills is to stay calm and calm down.

Many programmers tend to skip the basic part to learn the advanced part if they want to eat a fat girl. In the long run, the confidence in continuing to learn will become increasingly lost, but they do not want to give up due to various pressures, as a result, the pressure is getting higher and higher, and a programmer's common problem is described as "impetuous ".

This issue:

You will find that many programmers prefer to buy books and learn by themselves. In fact, this is a good habit. If you have enthusiasm and preferences for the IT industry, you will be persistent. If you are persistent, you will try your best to acquire knowledge, after the infinite volume changes, we skipped the persistent phase and finally found out the exit of the "programmer's way", so we started to start a business in desperate ways.

However, when buying books for self-study, some young programmers who have some preliminary technical experience may have a habit: they generally read chapter 2 First (to what extent do some first chapter of the book read, everyone knows ).

Then I found that the first two chapters were quite simple, So I skipped four to five chapters in the future. This jump is very interesting. Generally, after five or six chapters of technical books, will begin to talk about instances and applications.

As a result, the young man suddenly became interested. He decided to first look at the application instance, because the instance understands that he can submit his resume tomorrow. How efficient is it. As a result, the young man sat on the bed with smoke and warned his roommate to turn off the Dota sound.

An hour later, my roommate cheered-"turning the disk ". Let's look at the youth who have been sleeping in bed for a long time.

 

Body

PHP string Function

Many javaer and C # man often interact with each other on the Internet, for example, "Java is better or C # Is better ". This topic is even more intense and bottom-line than the "socialism" or "capitalism. At this time, one javaer took out the processing of the string object in Java and declared how efficient and structured it was, and C # Man took out the string object without being outputted, declare how convenient it is and how powerful its functions are. At this time, a phper accidentally entered the post. His weak intention was to join the post, so he listed the string function list in PHP and ended the above (meaning) lewd scene instantly.

I have to say that PHP's string function is indeed very powerful. The famous WordPress has used these functions almost to the extreme, and today this article will briefly talk about the string functions of PHP.

 The string function is built in PHP. You do not need to install or reference any function libraries.

This is good and neat. I like it.

Next, several commonly used string functions are revealed. If not, you can check the manual. I personally suggest that some functions do not need to be learned at all. You can check them again when using them. This is not comparable to Java or C.

I. Remove spaces, replace characters, and output

These three character processing functions are so common that insiders call them "sanbao". Why is sanbao? That's because Mastering these three treasures, coupled with some HTML knowledge, can be immediately used to create a page that is not very naive. At the same time, most functional functions need to use the old Three.

Space: trim. Removes spaces before and after a string.

The usage is also quite simple, such:

$ STR = 'nanjing's 8-second green light took a hundred meters of Chinese style cross-road MIS ';

$ STR = trim ($ Str); // the space around the characters "South" and "error" is removed.

This function seems simple, but there is a primary school question. It not only removes the common spaces of two strings, but also removes the \ t on both sides of the string (this product is the most annoying and clean in the whole solar system, I thought it was a space, but it was actually a tab) "" \ 0 ", that is, null." \ N "line break," \ x0b "-vertical list character," \ r "-Enter. So do not think that trim only uses spaces. In fact, the PHP kernel helps you kill many characters in obscurity.

If $ STR = trim ($ STR, '\ t'), it means that this trim only removes the \ t before and after the string, that is, the tab. If it encounters a common space, it will not be considered.

Output: it has three main functions: ECHO, print, and printf.

As for the difference: you only need to know that print is a little slower than echo, and printf can format the output. You don't have to make it clear. If you have to worry too much about everything, why should you learn PHP.

Here we will only talk about printf (print, you just need to use echo)

$ STR = "php ";
$ Number = 100;
Printf ("Happy Learning % S % u Day", $ STR, $ number );

The PhP100 days of happy learning will be output. % S represents the characters in order, and $ U represents the number. There are several % symbols, which will be followed by several parameters. If I write:

Printf ("Happy Learning % S % u Day", $ number, $ Str); then the Happy Learning 100 day will be output. PHP will first treat $ number as a string, that is, 100 as "100", and "php" as a numeric string. We all know that it is impossible to change PHP to a number, so it is 0.

String replacement function: str_replace

This function is very useful. How can it be used? Cyber police will let you fully understand why you should learn this function. You know.

$ STR = str_replace ("sensitive words", "I love my motherland", "XXX #%& &%@ one of them is sensitive words");

This means that I want to replace some sensitive words with "I love my motherland". Note that this function is followed by an optional count parameter. It is almost useless. It is used for counting, which indicates that it has been replaced several times in total. If you want to implement only a few replicas, use a regular expression. This chapter is not for the time being.

Some people say this function is inefficient. In fact, it doesn't matter if you process small data volumes on small websites. If you find that the simple str_replace character string on your website has greatly affected the server performance. Congratulations! You have to make a fortune.

 

Ii. strpos ()

This function is also very useful. Similar to indexof, which is commonly used in other languages.

For example, $ STR = 'This is my blog: www.shenyisyn.org. ';

So:

$ I = strpos ($ STR, "shenyisyn ");
Echo strval ($ I); // note why strval is used here. If $ I has a value, it is int type. You can directly output data on a webpage, which is invisible to the naked eye.

The output result is 38.

Here is a primary school question:

We cannot simply use if (strpos ($ STR, 'shenyisyn') to determine whether the string contains the word "shenyisyn.

Because if

$ STR = "shenyisyn is my Chinese abbreviation ";

The true value of strpos ($ STR, 'shenyisyn') is 0. In PHP, 0 is false. In fact, this match can be matched.

 So you need:

If (strpos ($ STR, 'shenyisyn') = false), this indicates that the specified 'shenyisyny' string exists.

In PHP, the difference between "=" is like the "Naked examination" during the college entrance examination. In addition to the Doctor's confirmation that we are = boys, we also need to be naked "=" to confirm, what if you just look like it? That is to say, "=" is the same as the type of the bid. In PHP, 0 = false, but 0! === False

This function is retrieved from left to right. The reverse is strrpos, which is from right to left, similar to lastindexof

The same. strpos is case sensitive. The undistinguished function is stripos. Another item from right to left is strripos.

The usage is the same, but the order is different. I will not go into detail.

Iii. Calculate the string length-strlen

This is a very engaging function. They all blame Qin Shihuang for taking it too early. If the whole world is unified, this problem will not be solved.

$ STR = "red ten sessions ";

Echo strval (strlen ($ Str ));

Do you know the results? Certainly not 3, not 6, but 9.

Let's take a look: strlen ("red ten sessions"); by default, each Chinese character is counted as 3 bytes. The entire length is 6. Both English and numbers are 1 byte. If it is changed to strlen ("Red 10"), it is 3 + 1 + 1 + 3 = 8 bytes.

This is required if you want to convert the data into 1 byte in English and 2 bytes in Chinese.

Echo mb_strlen ($ STR, 'utf8'); // make sure that the library mbstring is enabled. It is disabled by default.

Of course, the second parameter of mb_strlen can also be GBK or gb2312. This is not recommended and will confuse your life. Besides, utf8 is the basic method for developing websites. If your website's use of large libraries such as utf8 is under great pressure and cannot run properly, congratulations again. Since it was sent, I don't care how many servers I bought.

Iv. strip_tags remove HTML tags

The official Chinese explanation uses "stripping", which is a good word. Use this sharp word. What is stripping? For example, when you wear a really cool T-shirt on the way to the Organization, a rogue suddenly ran over and stripped your T-shirt and ran it on your own, so you can only go back to work in the Unit, and the colleagues in the unit smiled at you for a whole day.

This is the effect. It is only stripped and does not filter the content contained in HTML or XML tags.

For example, $ STR = "an old man donated 0.1 million <strong> after being misappropriated </strong>, he was very sad ";

Then $ STR = strip_tags ($ Str );

Echo $ STR; then an old man donated 0.1 million to be misappropriated, and he was very sad.

The effect of this function is that it will not delete the characters in the middle of the <strong> MARK "misappropriated", or else it will become "an old man donated 0.1 million kindly, he is very sad.

This function is amazing. It greatly reduces the difficulty and complexity of developing web programs. It is due to the fact that PHP is born for the web.

Today we will talk about these functions. Continue later ....

If anything is inappropriate, I hope to make positive corrections.

 

Not yet to be continued ....

Note: The reprint of this series of chapters must be approved in writing by me unless I am spontaneous. Otherwise, I reserve the right to complain and despise.

Author: Shen Yi


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.