2011-01-27 3 views
1

私はpreg_replaceスクリプトを作成しましたが、今ではカウント関数を追加したいと思います!preg_replaceを作る方法は、置換回数のカウント機能を持っていますか?

MY CODE

$replace = 'ISO Burning Programs/[email protected] ISO Burner 2.1.0.0/SPTDinst-v162-x86.exe'; 
$result=preg_replace('/[^0-9^A-Z^a-z-*… ,;[email protected]{}#<>""=-^:()\[\]]/', '<br/>', $replace); 
echo $result; 

OUTPUT

ISO Burning Programs 
[email protected] ISO Burner 2.1.0.0 
SPTDinst-v162-x86.exe 

しかし、私は

1 ISO Burning Programs 
2 [email protected] ISO Burner 2.1.0.0 
3 SPTDinst-v162-x86.exe 

をIS-たい出力は誰も私を助けることはできますか?
ありがとうございます!!!!!!

+3

方が良いと思いませんか'str_replace( '/'、 '
'、$ replace);' ...? – BoltClock

答えて

1

またあなたはこの行うことができます:あなたはpregでこれを行うにしたい場合は、あなたがpreg_replace_callbackを使用する必要があると思います

$replace = 'ISO Burning Programs/[email protected] ISO Burner 2.1.0.0/SPTDinst-v162-x86.exe'; 
$result = explode('/', $replace); 

foreach($result as $i => $value) 
    printf("%d %s<br />", ++$i, $value); 
0
preg_replace has a count function in it, the -1 is limit (unlimited) and $count is the number of replacement. just FYI. 

    $result=preg_replace('/[^0-9^A-Z^a-z-*… ,;[email protected]{}#<>""=-^:()\[\]]/', '<br/>\n', $replace, -1, $count); 

    $a = explode("\n", $result); 
    $i=1; 
foreach($a as $res) 
    { 
    echo $i . " " . $res; 
$i++; 
    } 
1

を:

$result = preg_replace_callback('/([^\/]*)(\/|$)/', function($matches){ 
    static $count = 0; 
    $count++; 
    return !empty($matches[1]) ? $count.' '.$matches[1].'<br/>' : ''; 
}, $replace); 
関連する問題