note 83142 deleted from function.preg-replace by felipe
| From: | felipe@php.net | Date: | Wed, 14 May 2008 18:22:20 +0000 |
| Subject: | note 83142 deleted from function.preg-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139383@lists.php.net to get a copy of this message | ||
Note Submitter: chris at online dash buddies dot com
----
Here's a problem I'm having with preg_replace. I'm looking up a value associated with
a string and replacing it with the associated id for better performance when pushed to production.
When I deal with escapable or special characters, I run into the problem of being able to detect
them (preg_match_all) but I can't replace them!!!!!!!!
[exec] line: <span><?php echo __('Password') ?> <?php echo
__('Wel\"come,') ?></span>
[exec] replace: Array
[exec] (
[exec] [0] => 2
[exec] [1] => 8
[exec] )
[exec] pattern: Array
[exec] (
[exec] [0] => /Password/
[exec] [1] => /Wel\"come,/
[exec] )
[exec] finished line:: <span><?php echo __('2') ?> <?php echo
__('Wel\"come,') ?></span>
[exec]
[exec] Replacing 2 keys in..................
Not sure why it's doing this.
Here's the code.
<?php
private function replaceLine($linenum, $line) {
//Local or make member variables????
$regs = NULL;
$count = preg_match_all($this->_pattern, $line, $regs);
$replacements = Array();
$patterns = Array();
if ($count) {
foreach ($regs[1] as $val) {
$replacements[] = array_search($val, $this->_xliff_data);
$patterns[] = "/$val/";
}
fwrite($this->_output_file, preg_replace($patterns, $replacements, $line));
echo "Replacing $count keys in $this->_filename --
Line:".($linenum+1)."\n";
}
else {
fwrite($this->_output_file, $line);
}
}
?>