Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php将字符串转换为csv文件,_Php_Regex_String_Csv - Fatal编程技术网

php将字符串转换为csv文件,

php将字符串转换为csv文件,,php,regex,string,csv,Php,Regex,String,Csv,背景 下面是由另一个程序创建的文件中包含的单个字符串 实际字符串输出 site,monster,cat,name, <br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Anchiornis<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Pelecanimimus<br/>`site`='Lochness',`mon

背景 下面是由另一个程序创建的文件中包含的单个字符串

实际字符串输出

site,monster,cat,name,   <br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Anchiornis<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Pelecanimimus<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Sinosauropteryx prima<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Protarchaeopteryx robusta
我的伪代码如下:使用preg_replace()和str_replace(),str_split()删除以下字符串 1.将
替换为\n(新行) 2.删除
和'eg
站点'='之间的任何内容,因为它们与标题行相同 3.将最后一列拆分为cat和name字段

尝试的代码 我尝试了许多方法来移除/替换不需要的字符串等部分,但未能成功移除所有需要的字符串。 我的正则表达式经验对于简单的单个字符很好,但对于复杂的字符串却不太好。下面的代码显示了我的一次尝试

    <?php

// replace carriage returns with new lines
$str = $re_html;

function br2nl($str) {
    $str = preg_replace("/(\r\n|\n|\r)/", "", $str);
    return preg_replace("=<br */?>=i", "\n", $str);
    }
br2nl($str);
echo $str; 
?>


作为php的新手,非常感谢任何易于理解的解决方案

字符串非常复杂,我会小心使用正则表达式,这样做的诀窍是:

<?php

$string = "site,monster,cat,name,<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Anchiornis<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Pelecanimimus<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Sinosauropteryx prima<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Protarchaeopteryx robusta";

$byLine = explode('<br/>', $string);
$keys = explode(',', substr(array_shift($byLine), 0, -1));

$csv = implode(', ', $keys) . "\n";

$lineCount = count($byLine);
for($i = 0; $i < $lineCount; $i++)
{
    $entry = explode(',', $byLine[$i]);
    $count = count($entry);
    for($j = 0; $j < $count; $j++)
    {
        $value = explode('`=\'', $entry[$j])[1];
        $lastApos = strrpos($value, '\'');
        if($lastApos === strlen($value) - 1)
        {
            $csv .= substr($value, 0, -1) . ', ';
        }
        else
        {
            $csv .= implode(', ', explode('\'', $value));
        }
    }
    if(($i + 1) < $lineCount)
    {
        $csv .= "\n";
    }
}

var_dump($csv);
编辑:如果出于论证的目的,您确实想使用疯狂的正则表达式,那么这就是您需要的:

<?php 

$string = "site,monster,cat,name,<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Anchiornis<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Pelecanimimus<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Sinosauropteryx prima<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Protarchaeopteryx robusta";

$regex = "#<br/>\`\w*\`=\'(\w*)',\`\w*\`=\'(\w*)',\`\w*\`=\'(\w*)'([\w\s]*)#";
$replace = "\n$1, $2, $3, $4";

$keyPos = strpos($string, ',<br/>');

$keys = str_replace(',', ', ', substr($string, 0, $keyPos));
$values = substr(preg_replace($regex, $replace, substr($string, $keyPos)), 2);

var_dump($keys . "\n" . $values);
您可以使用以下代码:

$str = preg_replace_callback("~(?>(?>',|(,?+\s*+<br/>))[^']++)?'|,~", 
    function ($m) { return (isset($m[1]))? "\n":', '; }, $str);
$str=preg\u replace\u回调(“~(?>(?>),|(,?+\s*+
)[^']++)?”,“, 函数($m){return(isset($m[1])?“\n”:“,”;},$str);
字符串是否真的看起来像所有那些反勾号和br标记,最后一个元素没有用引号括起来?如果没有,试着把它放在一个没有其他格式的代码块中。如果是这样的话,你不能改变它吗?因为那只是一个可怕的混乱,你永远无法充分维护。你是正确的,这就是字符串的样子。a)你是正确的,这就是字符串的样子。如何将其放入代码块中?b) 我需要一个csv格式,因为这是其他脚本下线使用的格式。c) 这个文件是从一个已经存在的mysql ajax php程序生成的,这个程序非常复杂,我试过了,但是我认为更改输出会更容易。谢谢,我会尝试一下并很快更新你,我知道这是一个复杂的字符串,但是遗留程序更复杂,哈哈@GeorgeThompson如果您更喜欢,我已经在正则表达式解决方案中添加了hi@MrLore我运行了脚本并得到以下错误解析错误:语法错误,意外'['第17行的/Applications/XAMPP/xamppfiles/htdocs/string2csv2.php中有什么想法吗?我目前正在尝试解决这个错误。Thanks@GeorgeThompson这是第一个吗?我的正则表达式甚至没有第17行是的,它在第一个上,当你发布第二个解决方案时我正在更新,第二个很有用我想得到第一个script正在工作,因为我需要一点时间才能理解正则表达式,谢谢。
<?php 

$string = "site,monster,cat,name,<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Anchiornis<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Pelecanimimus<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Sinosauropteryx prima<br/>`site`='Lochness',`monster`='dinasour',`cat`='Feathered'Protarchaeopteryx robusta";

$regex = "#<br/>\`\w*\`=\'(\w*)',\`\w*\`=\'(\w*)',\`\w*\`=\'(\w*)'([\w\s]*)#";
$replace = "\n$1, $2, $3, $4";

$keyPos = strpos($string, ',<br/>');

$keys = str_replace(',', ', ', substr($string, 0, $keyPos));
$values = substr(preg_replace($regex, $replace, substr($string, $keyPos)), 2);

var_dump($keys . "\n" . $values);
string 'site, monster, cat, name
Lochness, dinasour, Feathered, Anchiornis
Lochness, dinasour, Feathered, Pelecanimimus
Lochness, dinasour, Feathered, Sinosauropteryx prima
Lochness, dinasour, Feathered, Protarchaeopteryx robusta' (length=221)
$str = preg_replace_callback("~(?>(?>',|(,?+\s*+<br/>))[^']++)?'|,~", 
    function ($m) { return (isset($m[1]))? "\n":', '; }, $str);