note 104318 added to function.preg-replace
| From: | someuser at dot dot com | Date: | Wed, 08 Jun 2011 23:10:35 +0000 |
| Subject: | note 104318 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180343@lists.php.net to get a copy of this message | ||
Replacement of line numbers, with replacement limit per line.
Solution that worked for me.
I have a file with tasks listed each starting from number, and only starting number should be
removed because forth going text has piles of numbers to be omitted.
56 Patient A of 46 years suffering ... ...
57 Newborn of 26 weeks was ...
58 Jane, having age 18 years recollects onsets of ...
...
587 Patient of 70 years ...
etc.
<?php
// Array obtained from file
$array = file($file, true);
// Decompile array with foreach loop
foreach($array as $value)
{
// Take away numbers 100-999
// Starting from biggest
//
// % Delimiter
// ^ Make match from beginning of line
// [0-9] Range of numbers
// {3} Multiplication of digit range (For tree digit numbers)
//
if(preg_match('%^[0-9]{3}%', $value))
{
// Re-assing to value its modified copy
$value = preg_replace('%^[0-9]{3}%', '-HERE WAS XXX NUMBER-', $value, 1);
}
// Take away numbers 10-99
elseif(preg_match('%^[0-9]{2}%', $value)) {
$value = preg_replace('%^[0-9]{2}%', '-HERE WAS XX NUMBER-', $value, 1);
}
// Take away numbers 0-9
elseif(preg_match('%^[0-9]%', $value)) {
$value = preg_replace('%^[0-9]%', '-HERE WAS X NUMBER-', $value, 1);
}
// Build array back
$arr[] = array($value);
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 91.213.119.254
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/104318
Del: integrated -- https://master.php.net/note/delete/104318/integrated
Del: useless -- https://master.php.net/note/delete/104318/useless
Del: bad code -- https://master.php.net/note/delete/104318/bad+code
Del: spam -- https://master.php.net/note/delete/104318/spam
Del: non-english -- https://master.php.net/note/delete/104318/non-english
Del: in docs -- https://master.php.net/note/delete/104318/in+docs
Del: other reasons-- https://master.php.net/note/delete/104318
Reject -- https://master.php.net/note/reject/104318
Search -- https://master.php.net/manage/user-notes.php