从字符串中提取数据以存储到变量中

时间:2017-05-22 14:04:44

标签: php search preg-match

我需要找到每个字符串的作者,日期和正文(参见下面的示例)并将它们分别存储到变量中。我知道它会涉及正则表达式,但我还不足以弄清楚如何计算可能包含逗号和其他非字母字符的字符串。

示例字符串:

  

{作者= user1020,日期= 2017年1月7日,身体=我有booda圆顶(   同一个!)十年,它仍然很强大。没什么疯狂的   我觉得他们很欣赏隐私。}

并将其存储在如下变量中:

$author = "user1020";
$date = "1/7/2017";
$body = "I've had the booda dome (the same one!) for ten years and it's still going strong. Nothing crazy or fancy but I think they appreciate the bit of privacy .";

4 个答案:

答案 0 :(得分:1)

可能有一种更好的方法,并且没有针对不同情况的检查,但有一个想法让它运作:

$mString = "{Author=user1020, Date=1/7/2017, Body=I've had the booda dome (the same one!) for ten years and it's still going strong. Nothing crazy or fancy but I think they appreciate the bit of privacy .}";


$mString = returnToArray($mString);

echo "<pre>";
print_r($mString);
echo "</pre>";

function returnToArray($mString){

    $mFinalArray = null;

    $mString = str_replace('{','',$mString);
    $mString = str_replace('}','',$mString);

    $mString = explode(',', $mString);

    foreach($mString as $string){

        if(strpos(strtolower($string), 'author') !== false){ // If you find author in string 
            $mFinalArray['author'] = explode('=',$string)[1]; // Explode at '=' and take the "right" part (in fact it becomes an array and [1] is the right part of string in the specific example
        }
        else if(strpos(strtolower($string), 'date') !== false){ // If you find date in string 
            $mFinalArray['date'] = explode('=',$string)[1];
        }
        else if(strpos(strtolower($string), 'body') !== false){  // If you find body in string 
            $mFinalArray['body'] = explode('=',$string)[1];
        }

    }

    return $mFinalArray;

}
  

输出:

Array
(
    [author] => user1020
    [date] => 1/7/2017
    [body] => I've had the booda dome (the same one!) for ten years and it's still going strong. Nothing crazy or fancy but I think they appreciate the bit of privacy .}
)

至于“检查”......例如,如果您更改$mString正文部分并将“作者”添加到该字符串,则上述操作无效。一个想法是更改所有ifs并在最后添加=,如'author=',因此总会有=但不完全是我们想要的。

答案 1 :(得分:1)

如果你真的想要使用正则表达式并且你真的确定你的字符串具有这种确切的形状,那么对于作者你可以这样做:

/Author=(\w+),/g

Example

日期:

/Date=(.+),/g

Example

身体:

/Body=(.+)}/g

Example

答案 2 :(得分:1)

使用PHP的正则表达式解决方案

<?php

$string = "{Author=user1020, Date=1/7/2017, Body=I've had the booda dome (the same one!) for ten years and it's still going strong. Nothing crazy or fancy but I think they appreciate the bit of privacy .}";
$info = Array();

if (preg_match('/{Author=(?<author>.*), Date=/', $string, $matches)) {
    $info += $matches;
}

if (preg_match('/Date=(?<date>.*), Body=/', $string, $matches)) {
    $info += $matches;
}

if (preg_match('/Body=(?<body>.*)}/', $string, $matches)) {
    $info += $matches;
}

// to see output
// print_r($info)

$author = $info['author'];
$date = $info['date'];
$body = $info['body'];

?>

答案 3 :(得分:1)

让我们不要错过一个很好的机会来教授一些优秀的单线人员!

输入:

$in="{Author=user1020, Date=1/7/2017, Body=I've had the booda dome (the same one!) for ten years and it's still going strong. Nothing crazy or fancy but I think they appreciate the bit of privacy .}";

方法:

list($author,$date,$body)=preg_split('/(^|,)[^=]+?=/',substr($in,0,-1),null,PREG_SPLIT_NO_EMPTY);

输出:

echo "$author<br>$date<br>$body";
/*
user1020
1/7/2017
I've had the booda dome (the same one!) for ten years and it's still going strong. Nothing crazy or fancy but I think they appreciate the bit of privacy .
*/

我的正则表达式模式在每个所需子字符串之间展开字符串并按顺序返回它们,没有捕获组或包含“全字符串匹配”的数组,如preg_match()。在拆分之前,我必须手动撕掉尾随}。当然,list()会创建所需的变量。

P.S。如果有人担心准确性(或者如果其中一个目标子串中有一个符号可能会影响我的分裂模式),这个更为文字的替代方案将提供相同的输出(并处理尾随}):< / p>

list($author,$date,$body)=preg_split('/\{Author=|, Date=|, Body=|\}/',$in,null,PREG_SPLIT_NO_EMPTY);

对于任何不想让preg_match()与捕捉群体保持一致的人......

list(,$author,$date,$body)=preg_match('/Author=([^,]*), Date=([^,]*), Body=([^}]*)/',$in,$out)?$out:['','','',''];

说明:preg_match返回true | false,因此它被用作内联条件,其中全字符串和捕获数据在true上传递,空值在false上传递。 list()有一个空的第一个变量,因为我们不需要为fullstring匹配值赋值。