在PHP中将字符串剪切成不同的部分?

时间:2014-02-18 15:14:52

标签: php html function

我对PHP很新,我需要根据值而不是字符数将字符串剪切成某些部分。

我听说过爆炸功能和子串,但我不知道如何使用它来达到我想要的效果。

我有一个输出字符串:
Name: example Email: e.g@example.com 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...

为了审美目的,我希望在单独的行中显示此信息。

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

6 个答案:

答案 0 :(得分:3)

如果给定字符串是常规格式的 始终 ,则可以使用正则表达式来实现此目的。

使用preg_split()可能构成基础:

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

正则表达式使用positive lookahead断言将任意大写字母前面的点拆分给定字符串。

如果我们在数组上执行print_r(),您可以看到以下输出:

Array
(
    [0] => Name: example 
    [1] => Email: e.g@example.com 
    [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或类似方法来传输数据。这样更容易,也更少了 - 你可以确定它有效。

答案 1 :(得分:2)

这有效 -

$regex = "/[A-Z][^A-Z]*?\:\s.*?(?(?=[A-Z][^A-Z]*?\:)|$)/";
$string = "Name: Example Email: e.g@example.com 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: e.g@example.com ' (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)

*/

答案 2 :(得分:0)

使用像这样的简单字符串操作函数&让你的生活变得简单..

<?php
      $strx='Name: example Email: user@example.com 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
?>

答案 3 :(得分:0)

通常,explode()函数是您正在寻找的函数,但它在您的示例中效果不佳,因为您缺少一些用于分离的唯一字符。有类似的东西:

Name: example; Email: e.g@example.com; Website: http://example.com/

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

答案 4 :(得分:0)

这是一个REGEX,它将提取您要求的每个部分:

<?php

$string = "Name: example Email: e.g@example.com 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: ([-A-Z0-9_@.]+) Website: ([-A-Z0-9\/.:]+) /i', $string, $matches)) {
    $name = $matches[1];
    $email = $matches[2];
    $website = $matches[3];
}

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

答案 5 :(得分:0)

你可以拆分大写字母,假设你的标题始终以一个开头。

$text = "Name: example Email: e.g@example.com 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 />";
}