用PHP中的其他文本包围字符串的一些方法?(Some way to surround string with other text in PHP?)

编程入门 行业动态 更新时间:2024-10-15 20:23:38
用PHP中的其他文本包围字符串的一些方法?(Some way to surround string with other text in PHP?)

我有以下PHP函数:

public function createOptions($options, $cfg=array()) { $cfg['methodKey'] = isset($cfg['methodKey']) ? $cfg['methodKey'] : 'getId'; $cfg['methodValue'] = isset($cfg['methodValue']) ? $cfg['methodValue'] : 'getName'; $cfg['beforeKey'] = isset($cfg['beforeKey']) ? $cfg['beforeKey'] : ''; $cfg['beforeValue'] = isset($cfg['beforeValue']) ? $cfg['beforeValue'] : ''; $cfg['afterKey'] = isset($cfg['afterKey']) ? $cfg['afterKey'] : ''; $cfg['afterValue'] = isset($cfg['afterValue']) ? $cfg['afterValue'] : ''; $array = array(); foreach ($options as $obj) { $array[$cfg['beforeKey'] . $obj->$cfg['methodKey']() . $cfg['afterKey']] = $cfg['beforeValue'] . $obj->$cfg['methodValue']() . $cfg['afterValue']; } return $array; }

这是我在我的应用程序中用来从数组数据创建选择框的东西。 我最近添加了4个新的$ cfg变量,用于在选择框的键和值之前或之后添加字符串。 例如,如果我的下拉列表默认情况下看起来像“A,B,C”,我可以通过:

$cfg['beforeValue'] = 'Select '; $cfg['afterValue'] = ' now!';

并获得“立即选择!”,现在选择B!,现在选择C!

所以这工作正常,但我想知道是否有某种方式在PHP中完成这一行,而不是两个。 我认为必须有一种特殊的方式来做到这一点。

I have the following PHP function:

public function createOptions($options, $cfg=array()) { $cfg['methodKey'] = isset($cfg['methodKey']) ? $cfg['methodKey'] : 'getId'; $cfg['methodValue'] = isset($cfg['methodValue']) ? $cfg['methodValue'] : 'getName'; $cfg['beforeKey'] = isset($cfg['beforeKey']) ? $cfg['beforeKey'] : ''; $cfg['beforeValue'] = isset($cfg['beforeValue']) ? $cfg['beforeValue'] : ''; $cfg['afterKey'] = isset($cfg['afterKey']) ? $cfg['afterKey'] : ''; $cfg['afterValue'] = isset($cfg['afterValue']) ? $cfg['afterValue'] : ''; $array = array(); foreach ($options as $obj) { $array[$cfg['beforeKey'] . $obj->$cfg['methodKey']() . $cfg['afterKey']] = $cfg['beforeValue'] . $obj->$cfg['methodValue']() . $cfg['afterValue']; } return $array; }

It's something I use around my app to create select boxes from array data. I just recently added 4 new $cfg variables for adding strings before or after the key and value of the select box. So for example, if my drop down list looked like "A, B, C" by default, I can pass:

$cfg['beforeValue'] = 'Select '; $cfg['afterValue'] = ' now!';

and get "Select A now!, Select B now!, Select C now!"

So this works fine, but I was wondering if there was some way in PHP to accomplish this in one line sorta speak rather than two. I figure there must be a special way to do this.

最满意答案

首先,用这个来简化那些可怕的代码:

public function createOptions($options, array $cfg = array()) { $cfg += array( 'methodKey' => 'getId', 'methodValue' => 'getName', ... );

不需要所有的isset和重复的键名,一个简单的数组联合就可以做到。

其次,你可以使用类似sprintf东西:

$cfg['surroundingValue'] = 'Select %s now!'; echo sprintf($cfg['surroundingValue'], $valueInTheMiddle);

First, simplify that horrendous code with this:

public function createOptions($options, array $cfg = array()) { $cfg += array( 'methodKey' => 'getId', 'methodValue' => 'getName', ... );

No need for all the isset and repeated key names, a simple array union will do.

Secondly, you can use something like sprintf:

$cfg['surroundingValue'] = 'Select %s now!'; echo sprintf($cfg['surroundingValue'], $valueInTheMiddle);

更多推荐

本文发布于:2023-07-07 21:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   包围   文本   方法   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!