您好我正在尝试使用php explode来实现这一点,但我在执行此操作时遇到了麻烦。
这是我的代码
<?php
$capitals = array("A","B","C");
$word_break = "brandonBjakeCsullano";
$count = 0;
while ($count <=2)
{
$break = explode($capitals[$count++],$word_break)
}
echo $break[0];
echo "<br/>";
echo $break[1];
?>
使用上面的代码我可以得到这个结果:
brandonBjake
sullano
这就是我想要实现的目标
布兰登
杰克
sullano
感谢您的帮助。
任何策略都是可以接受的,而不是我的风格。 提前致谢
答案 0 :(得分:3)
尝试将str_replace
和explode
作为
$capitals = array("A","B","C");
$word_break = "brandonBjakeCsullano";
$word_break = str_replace($capitals, '/',$word_break);
print_r(explode('/',$word_break));//Array ( [0] => brandon [1] => jake [2] => sullano )
<强>被修改强> 如果你想回声,那么只需将其用作
echo implode('<br>',explode('/',$word_break));// instead of print_r
1)如果您使用print_r(explode('/',$word_break));
OK (0.008 sec real, 0.008 sec wall, 14 MB, 71 syscalls)
2)如果您使用echo implode("\n",explode('/',$word_break));
OK (0.008 sec real, 0.009 sec wall, 14 MB, 42 syscalls)
3)如果您使用$res= explode('/',$word_break);
foreach($res as $value){
echo $value."\n";
}
OK (0.012 sec real, 0.015 sec wall, 14 MB, 44 syscalls)
答案 1 :(得分:2)
这是另一个尝试:
#include<stdio.h>
int main(void)
{
int flx=0,fly=0;
char a,b[10][100];
while(1)
{
a=getchar();
if(a==EOF) exit(0);
else if(a=='\n')
{
flx++;
fly=0;
}
else
{
b[flx][fly++]=a;
}
}
}
<强>输出: - 强>
<?php
$capitals = array("A","B","C");
$string = "brandonBjakeCsullano";
$count = 0;
$capitals = array("A","B","C");
$string = "brandonBjakeCsullano";
$string = str_replace($capitals, '/',$string);
$res= explode('/',$string);
foreach($res as $value){
echo $value."<br>";
}
?>
供参考Click Here
答案 2 :(得分:1)
总是好的旧preg_split
允许您使用正则表达式将字符串拆分为数组。
像(未经测试)的东西:
$names = preg_split( '/[A-Z]/', 'brandonBjakeCsullano' );
print_r( $names );
答案 3 :(得分:0)
试试这个:
<?php
$capitals = array("A","B","C");
$word_break = "brandonBjakeCsullano";
$arr=str_split($word_break);
for($i=0;$i <=strlen($word_break);$i++){
for($j=0;$j<=2;$j++){
if($arr[$i]!=$capitals[$j]){
$flag=TRUE;
}
else{
$flag=FALSE;
echo '<br>';
break;
}
}
if($flag==TRUE){
echo $arr[$i];
}
}
?>