我想在php中替换整行

时间:2014-06-07 17:03:12

标签: php design-patterns

我想替换

'HASH_KEY' => string '1234567890123' (length=13)

通过

'HASH_KEY' => hide

但我无法得到它。我试过这个:

'SESSION_TIME' => int 10
'HASH_KEY' => string '1234567890123' (length=13)
'DB_HOST' => string 'localhost' (length=9)
'DB_USER' => string 'user' (length=4)
'DB_PASS' => string 'pass' (length=4)

preg_replace("/\'HASH_KEY\'[a-zA-Z0-9\=\>\'\ ]*$/", 'HASH_KEY => hide', $string);

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:2)

首先你需要以某种方式处理函数结果,例如。将其分配给变量。 使用.表示(几乎)any character wildcard*来定义它应该发生0..n次。在末尾附加多行标记m

$output = preg_replace("/'HASH_KEY'.*=\>.*$/m", "'HASH_KEY' => hide", $string);

请尝试完整的脚本:

<?php

$string = "
'SESSION_TIME' => int 10
'HASH_KEY' => string '1234567890123' (length=13)
'DB_HOST' => string 'localhost' (length=9)
'DB_USER' => string 'user' (length=4)
'DB_PASS' => string 'pass' (length=4)
";

$output = preg_replace("/'HASH_KEY'.*=\>.*$/m", "'HASH_KEY' => hide", $string);

echo "<pre>" . $output . "</pre>"; 

?>