任何等同于 .= 用于添加到 PHP 中字符串开头的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7111816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:01:59  来源:igfitidea点击:

Any equivalent to .= for adding to beginning of string in PHP?

php

提问by yuttadhammo

Just wondering if there is something like .= for adding text to the beginning of a string, e.g.:

只是想知道是否有类似 .= 的东西用于将文本添加到字符串的开头,例如:

$foo =. 'bar';

which doesn't work.

这不起作用。

Edit: example was originally $foo =. $bar;which can be achieved with $bar .= $foo;

编辑:示例最初$foo =. $bar;可以通过$bar .= $foo;

回答by Eric V

Nope. But you can do

不。但是你可以做

$foo = "bar" . $foo

回答by Aaron

You could always make your own function for that:

您始终可以为此创建自己的功能:

function prepend($string, $chunk) {
     if(!empty($chunk) && isset($chunk)) {
        return $string.$chunk;
     }
     else {
        return $string;
     }
}

$stringwould be the piece that you want to prepend and $chunkwould be the text that you want something prepended onto it.

$string将是您想要添加的部分,并且$chunk将是您想要在其上添加某些内容的文本。

You could say the checks are optional, but by having that in there you don't need to worry about passing in a null value by accident.

您可以说这些检查是可选的,但是通过在其中进行检查,您无需担心意外传入空值。

回答by fyrye

I know this was asked/answered a while ago, but providing this answer as it is functionally equivalent despite it not being an assignment operator and no one commented on its usage for general string concatenation.

我知道不久前有人问过/回答过这个问题,但是提供这个答案是因为它在功能上是等效的,尽管它不是赋值运算符,并且没有人评论它在一般字符串连接中的用法。

You may want to look into the use of the sprintf(documentation) family of functions for string concatenation. It provides a lot more sanitization and usability than just combining two strings with assignment operators.

您可能需要研究使用sprintf文档)函数系列进行字符串连接。它提供了更多的清理和可用性,而不仅仅是将两个字符串与赋值运算符组合在一起。

$foo = 'foo';

$append = sprintf('%1$s%2$s', $foo, 'bar');
var_dump($append);
/* string(6) "foobar" */

$prepend = sprintf('%1$s%2$s', 'bar', $foo);
var_dump($prepend);
/* string(6) "barfoo" */

$prependInvert = sprintf('%2$s%1$s', $foo, 'bar');
var_dump($prependInvert);
/* string(6) "barfoo" */

$wrap = sprintf('%2$s%1$s%2$s', $foo, 'bar');
var_dump($wrap);
/* string(6) "barfoobar" */

I normally use vsprintf, since working with arrays is easier to manage value positions than individual arguments.

我通常使用vsprintf,因为使用数组比单个参数更容易管理值位置。

$vprepend = vsprintf('%2$s%1$s', array('foo', 'bar'));
var_dump($vprepend);
/* string(6) "barfoo" */

Also with an array of values, one can simply implodethe resultant set of values for simple string concatenation.

同样对于一组值,我们可以简单地implode将结果集用于简单的字符串连接。

 var_dump(implode('', array('bar', 'foo')));
 /* string(6) "barfoo" */

回答by sde-simon

You can wrap the built-in function substr_replace, where the arguments $start and $length can be set to 0, which prepends the $replacement to $string and returns the result, like so:

您可以包装内置函数substr_replace,其中参数 $start 和 $length 可以设置为 0,这将 $replacement 附加到 $string 并返回结果,如下所示:

function prepend(& $string, $prefix) {
    $string = substr_replace($string, $prefix, 0, 0);
}

An example usage of the helper function would be:

辅助函数的示例用法是:

$email_message = "Jonathan";
$appropriate_greeting = "Dear ";
prepend($email_message, $appropriate_greeting);
echo $email_message;

If you are into procedural programming, that is.

如果您喜欢过程式编程,那就是。

回答by robrecord

Turning Blakethepatton's commentinto an answer (thank you), a way to do this more neatly for longer variable names is by using a referenceto the variable as follows:

Blakethepatton 的评论转化为答案(谢谢),对于更长的变量名称更巧妙地执行此操作的方法是使用对变量的引用,如下所示:

$f = &$foo; $f = "bar{$f}";

This will save you typing over the answer by Eric Vif your original variable name is 12 characters or longer.

如果您的原始变量名称为 12 个字符或更长,这将节省您输入Eric V答案

An alternative on one line:

一行的另一种选择:

$f = 'bar' . ($f = &$foo);