化背景 下面是一个包含在由另一个程序创建的文件中的单个字符串。
实际字符串输出
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
我已将下面的字符串拆分为更容易阅读,但实际字符串如上所示 没有换行符
Easeier读取字符串输出
现场,怪物,猫,名字,
site
=&#39; Lochness&#39; monster
=&#39; dinasour&#39; cat
=&#39;羽衣&#39;近鸟龙属
site
=&#39; Lochness&#39; monster
=&#39; dinasour&#39; cat
=&#39;羽衣&#39;似鹈鹕龙属
site
=&#39; Lochness&#39;,monster
=&#39; dinasour&#39;,cat
=&#39; Feathered&#39; Sinosauropteryx prima
site
=&#39; Lochness&#39;,monster
=&#39; dinasour&#39;,cat
=&#39;羽毛&#39; Protarchaeopteryx robusta
所需的输出 我想将其转换为csv文件/字符串,如下所示,供其他脚本使用。
site, monster, cat, name
Lochness, dinasour, Feathered, Anchiornis
Lochness, dinasour, Feathered, Pelecanimimus
Lochness, dinasour, Feathered, Sinosauropteryx prima
Lochness, dinasour, Feathered, Protarchaeopteryx robusta
我的伪代码如下使用preg_replace()和str_replace(),str_split()来删除以下字符串
1.替换为\ n(新行)
2.删除and ' eg
site` =&#39;之间的任何内容因为它们与标题行相同
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的新手任何易于理解的解决方案非常感谢!
答案 0 :(得分:0)
这个字符串非常复杂,以至于我对使用正则表达式持谨慎态度,这就是诀窍:
<?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);
产地:
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)
编辑:如果为了争论而你确实想要使用疯狂的正则表达式,那么这就是你需要的:
<?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)
答案 1 :(得分:0)
您可以使用此代码:
$str = preg_replace_callback("~(?>(?>',|(,?+\s*+<br/>))[^']++)?'|,~",
function ($m) { return (isset($m[1]))? "\n":', '; }, $str);