Home >Backend Development >PHP Tutorial >[PHP source code reading] strtolower and strtoupper functions, strtoupper_PHP tutorial

[PHP source code reading] strtolower and strtoupper functions, strtoupper_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:51:201274browse

[PHP source code reading] strtolower and strtoupper functions, strtoupper

Among the string operation functions, string case conversion is also a relatively commonly used function, and its underlying implementation is also It’s relatively simple, let’s find out below.

I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.

strtolower

<p>string strtolower ( string $string )</p>

Convert the string to lowercase characters.

strtoupper

<p>string strtoupper ( string $string )</p>

Convert the string to uppercase characters.

Run the example

<span>$str</span> = 'Hello World'<span>;
</span><span>$new_str</span> = <span>strtolower</span>(<span>$str</span>); <span>//</span><span> hello world</span>

<span>$str</span> = 'hello world'<span>;
</span><span>$new_str</span> = strupper(<span>$str</span>); <span>//</span><span> HELLO WORLD</span>

Code running steps

<p>拷贝一份字符串</p>
<p>php_strtolower/php_strtoupper进行转换</p>

Source code interpretation

The core operations of the two functions are similar. Let’s talk about strtolower, the other one is similar.
The core code of the php_strtolower function is as follows:

c = (unsigned <span>char</span> *<span>)s;
e </span>= c+<span>len;

</span><span>//</span><span> 遍历s,逐个变为小写</span>
<span>while</span> (c <<span> e) {
  </span>*c = tolower(*<span>c);
  c</span>++<span>;
}
</span><span>return</span> s;

This function traverses the entire string and converts it into lowercase characters one by one. This is also a classic pointer operation.

Original article with limited writing style and limited knowledge. If there is anything wrong in the article, please let me know.

If this article is helpful to you, please click to recommend it, thank you^_^

Finally, I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.

For more source code articles, please visit your personal homepage to continue viewing: hoohack

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1131142.htmlTechArticle[PHP source code reading] strtolower and strtoupper functions, strtoupper string operation function, string case conversion It is also a relatively commonly used function, and its underlying implementation is relatively simple,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn