2014-02-18 44 views
0

我对PHP相当陌生,我需要根据值而不是字符数将字符串剪切成特定的部分。在PHP中将字符串切成不同的部分?

我听说过爆炸函数和子字符串,但我不知道如何使用它来实现我想要的。

我有一个输出字符串:
Name: example Email: [email protected] Website: http://examples.com/ Comment: this is where the comment goes, we can't use explode because there's lots of spaces here Another comment: this is another comment Some random info: this is more info...

我想显示单独的行出于美观的目的这一信息。

有没有办法在特定单词之后但在另一个特定单词之前返回单词?

+0

你熟悉数组? – MonkeyZeus

+0

@Haudegen有所有应有的尊重,OP现在无法弄清楚爆炸和子串,你建议REGEX? – MonkeyZeus

+0

哈哈,我知道RegEx是什么,但我不知道如何使用它。我也知道子字符串是干什么的,但是这样会减少字符数量的字符串,我不是在寻找。爆炸也没有帮助,因为我没有分隔字符串的不同部分(例如name:name; email:email;) – user3313191

回答

0

使用这样&简单的字符串处理函数使乌拉圭回合的生活简单..

<?php 
     $strx='Name: example Email: [email protected] Website: http://example.com/'; 
     $str=stristr($strx,"Comment :",true);// Take all chars before 'Comments :' 
     $str= str_replace(": ",":",str); 
     $str = str_replace(" ","\n");   
     echo $str . '\n Comment : '. ; // Its 
     echo stristr($strx,"Comment :"); // Done 
?> 
+0

谢谢,但这不是我想要实现的... – user3313191

0

一般来说,explode()功能是你在找什么,但它不会在你的榜样工作也很好,因为你是错过了一些独特的分离字符。像这样的东西:

Name: example; Email: [email protected]; Website: http://example.com/ 

你可以例如爆炸字符串的分号。 explode() tutorial

+0

是的,这是问题,没有独特的性格,我不能使用空间,因为信息字段包含空格。 – user3313191

+0

@ user3313191:'explode()'不适用于这种情况。你可以使用'preg_split()'来代替。看看[我的答案](http://stackoverflow.com/a/21858534/1438393)。 –

0

这里是一个正则表达式,将拉出来,你所要求的各个部分:

<?php 

$string = "Name: example Email: [email protected] Website: http://examples.com/ Comment: this is where the comment goes, we can't use explode because there's lots of spaces here Another comment: this is another comment Some random info: this is more info... "; 

$name = ''; 
$email = ''; 
$website = ''; 

if (preg_match('/^Name: ([-A-Z \'.]+) Email: ([[email protected]]+) Website: ([-A-Z0-9\/.:]+) /i', $string, $matches)) { 
    $name = $matches[1]; 
    $email = $matches[2]; 
    $website = $matches[3]; 
} 

print ' 
    Name: '.$name.' 
    Email: '.$email.' 
    Website: '.$website; 
+0

随着更新的问题 - 最后有一个'评论:'块 - 所以如果你需要相应地调整RegExp(网站将杀死它后的行终止符) - 但否则是的,如果你需要操纵字符串没有比正则表达式IMO更好或更灵活的了。 – CD001

+0

已注意。我会做一个更新。 – Quixrick

0

你可能分裂一个大写字母,假设你的标题总是一个开始。

$text = "Name: example Email: [email protected] Website: http://examples.com/ Comment: this is where the comment goes, we can't use explode because there's lots of spaces here Another comment: this is another comment Some random info: this is more info..."; 
$test = preg_split('/(?=[A-Z])/', $text); 
foreach($test AS $line){ 
    echo $line."<br />"; 
} 
3

您可以使用正则表达式来实现这一目标,提供给定的字符串是总是在常规格式。

使用preg_split()可能形成的基础:

$arr = preg_split('/(?=[A-Z])/', $str, -1, PREG_SPLIT_NO_EMPTY); 

正则表达式分割点处给定的字符串立即使用positive lookahead断言任何大写字母之前。

如果我们做一个print_r()阵列上,你可以看到下面的输出:

Array 
(
    [0] => Name: example 
    [1] => Email: [email protected] 
    [2] => Website: http://examples.com/ 
    [3] => Comment: this is where the comment goes ... 
    [4] => Another comment: this is another comment 
    [5] => Some random info: this is more info... 
) 

你如何得到这个字符串的第一个地方?如果这是你的控制的东西,那么我强烈建议使用JSON或类似的传输数据。它的方式更容易,更少的错误 - 你可以肯定它的工作原理。

+0

+1为简单起见。我想知道,如果这个值的内容(假设是key:value)具有大写字母,分裂正则表达式将如何改变?我的意思是这样的 - “名称:例子”。无论如何要做到这一点使用拆分? – Kamehameha

+0

@Yah:有趣的问题。我会尽力找到解决方案!给我几分钟。 –

+1

@Yah:像这样的东西应该可以工作(?= [A-Z] [a-z] + \ s *:\ s *)'。查看演示:https://eval.in/102804 –

2

这工作 -

$regex = "/[A-Z][^A-Z]*?\:\s.*?(?(?=[A-Z][^A-Z]*?\:)|$)/"; 
$string = "Name: Example Email: [email protected] Website: http://examples.com/ Comment: this is where the comment goes, we can't use explode Because there's Lots of spaces here Another comment: this is another comment Some random info: this is more info"; 
if(preg_match_all($regex, $string, $matches)){ 
    var_dump($matches[0]); 
} 
/* 
OUTPUT 
array 
    0 => string 'Name: Example ' (length=14) 
    1 => string 'Email: [email protected] ' (length=23) 
    2 => string 'Website: http://examples.com/ ' (length=30) 
    3 => string 'Comment: this is where the comment goes, we can't use explode Because there's Lots of spaces here ' (length=98) 
    4 => string 'Another comment: this is another comment ' (length=41) 
    5 => string 'Some random info: this is more info' (length=35) 

*/