如何在不将PHP复制值从内存中的一个位置复制到另一个位置的情况下连接PHP字符串?

时间:2022-11-15 19:52:32

I made a function which takes any number of words for a parameter and capitalizes the first letter of each word.

我创建了一个函数,它为参数获取任意数量的单词,并将每个单词的第一个字母大写。

I looked at this site: http://www.carlconrad.net/en/2014/03/17/improving-php-performance/ and it states:

我查看了这个网站:http://www.carlconrad.net/en/2014/03/17/improving-php-performance/,它说:

Avoid string concatenations in loops. When placed in a loop, string concatenation results in the creation of large numbers of temporary objects and unnecessary use of the garbage collector. Both consume memory and can dramatically slow down script execution.

避免循环中的字符串连接。当放置在循环中时,字符串连接会导致创建大量临时对象并不必要地使用垃圾收集器。两者都消耗内存并且可以大大减慢脚本执行速度。

I'm trying to figure out how I can modify my code below to make it run faster. Is there another way to concatenate strings in PHP without relying on the value of the original?

我试图找出如何修改下面的代码以使其运行得更快。是否有另一种方法可以在不依赖原始值的情况下在PHP中连接字符串?

I know in raw C language, I could use the address pointer of the string plus an offset within defined bounds to add data without worrying about the original string being copied elsewhere which sources claim what PHP does during concatenation.

我知道在原始C语言中,我可以使用字符串的地址指针加上定义范围内的偏移量来添加数据,而不必担心原始字符串被复制到其他地方哪些来源声称PHP在连接期间做了什么。

Ideally, I want my string concatenation to work like the way this C code works (assume we are in the main() function here):

理想情况下,我希望我的字符串连接像这个C代码的工作方式一样工作(假设我们在这里的main()函数中):

char string[1000];
memcpy(string,'ABCD',4); //place ABCD at start
memcpy(string+4,'EFGH',4); //add EFGH to the string (no previous string copying required)

Just plain concatenation without manipulating the previous value of the string.

只是简单的连接而不操纵字符串的先前值。

This is my php code that needs advice on improvement:

这是我的PHP代码,需要改进建议:

function capitalize($words){
    $words=$words.' ';
    $returnedwords='';
    $eachword=explode(' ',$words);$numberofwords=count($eachword);
    if ($numberofwords >=1){
        $wordkey=array_keys($eachword);
        for($thiswordno=0;$thiswordno<$numberofwords;$thiswordno++){
            $word=$eachword[$wordkey[$thiswordno]];
            $returnedwords.=' '.strtoupper(substr($word,0,1)).strtolower(substr($word,1));
        }
        return substr($returnedwords,1);
    }
}

Any ideas how I can follow the websites recommendation to avoid string concatenation in loops like I have in mine?

任何想法我如何遵循网站建议,以避免像我在我的循环中的字符串连接?

1 个解决方案

#1


1  

In php it have function to make first word of string as capital.

在php中,它具有将字符串的第一个单词作为大写的功能。

example 1: ucword of string:

示例1:字符串的ucword:

$str = 'this is test value of data';
echo ucwords('this is test value of data');

output: This Is Test Value Of Data

输出:这是数据的测试值

example 2: create ucword string from array having many words:

示例2:从包含多个单词的数组创建ucword字符串:

$str = array(
    'this',
    'is',
    'test',
    'VALUE',
    'of',
    'Data'
);

$str = array_map('ucwords', array_map('strtolower', $str));

echo implode(' ', $str);

output: This Is Test Value Of Data

输出:这是数据的测试值

for more detail have a look at: PHP String Functions

有关更多详细信息,请参阅:PHP字符串函数

#1


1  

In php it have function to make first word of string as capital.

在php中,它具有将字符串的第一个单词作为大写的功能。

example 1: ucword of string:

示例1:字符串的ucword:

$str = 'this is test value of data';
echo ucwords('this is test value of data');

output: This Is Test Value Of Data

输出:这是数据的测试值

example 2: create ucword string from array having many words:

示例2:从包含多个单词的数组创建ucword字符串:

$str = array(
    'this',
    'is',
    'test',
    'VALUE',
    'of',
    'Data'
);

$str = array_map('ucwords', array_map('strtolower', $str));

echo implode(' ', $str);

output: This Is Test Value Of Data

输出:这是数据的测试值

for more detail have a look at: PHP String Functions

有关更多详细信息,请参阅:PHP字符串函数