1

我需要基于 simple 定期删除字符串的某些部分special characters like _ and |。这是我的代码:

<?php
$text = "a*a1.zip,a2.zip|b*b1.zip|c*c1.zip|d*d1.zip|e*e1.zip|f*f1.zip|g*g1.zip|h*h1.zip";
$expl = explode("|", $text);
print_r($expl);
?>

我需要删除所有alphabets and *'s, |'s这样我的输出应该看起来像:

a1.zip,a2.zip,b1.zip,c1.zip,d1.zip,e1.zip,f1.zip,g1.zip,h1.zip

我正在尝试使用preg_replace,但很难理解:(。还有其他选择吗?提前致谢...

4

3 回答 3

2

您可以改用preg_match,但您仍然需要让您的正则表达式正确,所以它不一定会更容易。如果您更愿意使用没有正则表达式的东西,请尝试双重爆炸

$text = "a*a1.zip,a2.zip|b*b1.zip|c*c1.zip|d*d1.zip|e*e1.zip|f*f1.zip|g*g1.zip|h*h1.zip";
$expl = explode("|", $text);
foreach ($expl as $part) {
    // PHP 5.4+
    $values[] = explode('*', $part)[1];

    // OR PHP < 5.4
    $tempvar = explode('*', $part);
    $values[] = $tempvar[1];

    // Choose one of the above, not both
}
$string = implode(',', $values);
于 2013-08-17T06:57:12.860 回答
1

试试这个。我没有测试这个。但你可以得到一个提示

<?php
    $text = "a*a1.zip,a2.zip|b*b1.zip|c*c1.zip|d*d1.zip|e*e1.zip|f*f1.zip|g*g1.zip|h*h1.zip";


    $expl = explode("|", $text);

    // YOU HAVE TO REMOVE a*, b*, c* which will be the first 2 characters after exploding. avoid this first 2 characters using substr

    foreach($expl as $key=>$value) {

        $result[]   = substr($value,2);

    }

    $result_string = implode(',', $result);

    ?>
于 2013-08-17T07:04:48.023 回答
1

单班轮无preg_

$result_string = implode(',',array_map(function($v){return substr($v,strpos($v,'*')+1);},explode('|',$text)));
于 2013-08-17T07:23:53.067 回答