我想获取一个姓名列表(来自 Wordpress)并获取姓名缩写并用“/”分隔。在大多数情况下,会有 2 个或更多名称。在某些情况下,只有 1 个名称。
例如:
约翰·多伊 简
·亚当
斯 迈克尔·麦克唐纳
应输出: JD/JA/MD
这是我迄今为止尝试找出答案的尝试。
foreach ( $result as $user )
{
$user_id = $user->user_id1;
$user = get_userdata( $user_id );
$name[] = $user->display_name;
}
$comma_names = implode('/', $name);
$words = explode("/", $comma_names);
$initials = "";
foreach ($words as $w) {
$initials .= $w[0];
}
echo $initials;
Run Code Online (Sandbox Code Playgroud)
显然,这仅输出每个名字的第一个字母。我需要在循环中使用额外的爆炸吗?
做这个的最好方式是什么?
我有一个文件,我们将在UNIX格式下调用info.txt,其中只有以下内容:
#Dogs
#Cats
#Birds
#Rabbits
Run Code Online (Sandbox Code Playgroud)
我正在反对它:
$filename = "info.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd);
$delimiter = "#";
$insideContent = explode($delimiter, $contents);
Run Code Online (Sandbox Code Playgroud)
现在一切看起来都很好,除非我显示数组我得到以下内容.
[0] =>
[1] => Dogs
[2] => Cats
[3] => Birds
[4] => Rabbits
Run Code Online (Sandbox Code Playgroud)
我检查了.txt文件以确保第一个#前面没有任何空格或隐藏字符,所以我不知道为什么会发生这种情况,除了我觉得我错过了一些非常简单的东西.有任何想法吗?
提前致谢!
我遇到的情况是我需要爆炸一个包含用户输入但是自定义定界符的字符串.我想确保用户不能在输入中输入分隔符,以免在错误的位置爆炸字符串.
做这个的最好方式是什么?是否有某种类型的过滤器我应该在用户数据上运行去除分隔符的出现?我认为除了创建一个独特的分隔符之外,还有更好的答案.
谢谢.
我有一个很长的字符串,像这样:
11070, 'EP_LQ-630', 'LQ-630, 24 pin, A4, 360 cps, USB 1.1&LPT', NULL, 6438, 1, 45, 1, 5184, 0, 20, NULL, NULL, 432, 1, 5088, 0, 424, 1, 1, 4, 1, NULL, NULL, NULL, NULL, NULL, NULL, 3, NULL, NULL, NULL, 370, 1, 369, 1, 368, 1, 367, 1, NULL, NULL, NULL, NULL, NULL, 1, '8443', NULL, NULL, NULL, '07.12.2011 18:16:28', NULL, NULL, '8471604', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 366, 1, ';6438;6432;4873;', NULL, NULL, NULL, NULL, NULL, NULL, NULL, …Run Code Online (Sandbox Code Playgroud) 假设我有这个字符串:
var1, var2, var3, "var4, test, test2", var5, "var6", var7
Run Code Online (Sandbox Code Playgroud)
如何使用explode()函数将它们放入这样的数组中
Array
(
[0] => var1
[1] => var2
[2] => var3
[3] => var4, test, test2
[4] => var4
[5] => var6
[6] => var7
)
Run Code Online (Sandbox Code Playgroud) <?php
$player = $_GET['player'];
$data = explode(" ", file_get_contents("http://hiscore.runescape.com/index_lite.ws?player={$player}"));
print_r($data);
?>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这会返回
Array ( [0] => 322788,1584,39425065 474211,75,1213741 424332,73,1044331 497032,75,1223804 518140,74,1127869 622836,53,146813 484601,51,114656 540925,65,452516 276405,78,1772587 614588,62,339049 85477,99,13069655 135438,86,3702239 69906,99,13034532 376565,60,294135 403376,55,173156 413268,62,349011 339269,50,103575 388661,52,125891 452907,52,134281 402625,50,102104 281390,50,109948 236592,63,377586 385212,50,103894 329955,50,104225 320731,50,103833 286842,50,101634 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 )
Run Code Online (Sandbox Code Playgroud)
检索的原始数据file_get_contents是
322790,1584,39425065 474214,75,1213741 424335,73,1044331 497037,75,1223804 518145,74,1127869 622844,53,146813 484604,51,114656 540930,65,452516 276407,78,1772587 614595,62,339049 85477,99,13069655 135439,86,3702239 69906,99,13034532 376567,60,294135 403379,55,173156 413272,62,349011 339272,50,103575 388664,52,125891 452909,52,134281 402629,50,102104 281392,50,109948 …Run Code Online (Sandbox Code Playgroud) 我从表单中得到以下字符串...
Opera"增加了跨平台硬件""踢屁股" - hi.
一般来说,我只是使用以下...
$p0 = explode(' ',$string);
Run Code Online (Sandbox Code Playgroud)
但是现在我要保持任何和所有报价运营商为单个阵列项目而不是让它们创造这样的个别项目"adds,cross-platform和hardware".
我想让那个字符串最终创建一个像这样的数组......
Array
(
[0] => 'Opera',
[1] => 'adds cross-platform hardware',
[2] => 'kicks butt',
[3] => '-hippies'
)
Run Code Online (Sandbox Code Playgroud)
我通常喜欢尽可能不使用正则表达式.
目前我正在爆炸一个字符串,.它按我喜欢的方式工作.唯一的问题是当.小数点出现时也会爆炸.有没有办法decimal从爆炸功能中排除点?
我目前的设置:你可以看到它.在两个数字之间爆炸
$String = "This is a string.It will split at the previous point and the next one.Here 7.9 is a number";
$NewString = explode('.', $String);
print_r($NewString);
output
Array (
[0] => This is a string
[1] => It will split at the previous point and the next one
[2] => Here 7
[3] => 9 is a number
)
Run Code Online (Sandbox Code Playgroud) 我有一系列颜色存储,如此呈现.如您所见,color_codes存储有哈希和逗号.
Array
(
[0] => Array
(
[0] => Array
(
[item_color] => Black
[color_codes] => #000000,#000000,
)
[1] => Array
(
[item_color] => Red
[color_codes] => #FF0033,
)
)
[1] => Array
(
[0] => Array
(
[item_color] => White
[color_codes] => #FFFFFF,
)
[1] => Array
(
[item_color] => Black
[color_codes] => #0C0C0C,#0C0C0C,
)
)
)
Run Code Online (Sandbox Code Playgroud)
是否可以使用explode删除每种颜色前面的#并用 - (破折号)符号替换逗号.
foreach通过每个项目实现这一目标吗?我已经尝试删除哈希,所以把我没有运气.
for ($i = 0; $i < count($colours); $i++) {
$colours[$i]['color_codes'] = str_replace('#', '', $colours[$i]['color_codes']);
}
Run Code Online (Sandbox Code Playgroud)
我的目标是,如果有两个例如,颜色看起来像这样 FFFFFF-FFFFFF-
嗨,我有以下字符串
$phones = "Samsung Galaxy S8~LG G6~iPhone 7 Plus~ Motorola Z2";
Run Code Online (Sandbox Code Playgroud)
我将字符串与爆炸分开
$myArray = explode('~', $phones);
Run Code Online (Sandbox Code Playgroud)
并在foreach循环中使用它
foreach($myArray as $value) {
echo '<li>'.$value.'</li>';
}
Run Code Online (Sandbox Code Playgroud)
结果是:
但我要做的是为除了最后一个元素之外的每个元素添加逗号并为数字添加括号.
我尝试过implode但是没有使用foreach循环
谢谢
explode ×10
php ×10
implode ×2
string ×2
comma ×1
foreach ×1
preg-replace ×1
split ×1
str-replace ×1