Re: [PHP] preg_replace strange behaviour, duplicates
Take a look at the negative assertions on this page:
http://us2.php.net/manual/en/regexp.reference.php
Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
Adz07 wrote:
I am trying to nail down a bit of code for changing processor names depending
on matches.
Problem i am having is the replacement takes place then it seems to do it
again replacing the text just replaced as there are similar matches
afterwards. example (easier)
$string = "The new Intel Core 2 Duo T8300";
$patterns = array("/Intel Core 2 Duo/","/Intel Core 2/");
$replacements = array("/Intel Core 2 Duo Processor Technology/","/Intel Core
2 Processor Technology/");
I would expect to get the following:
The new Intel Core 2 Duo Processor Technology T8300
but i get
The new Intel Core 2 Processor Technology Duo Processor Technology T8300
I can see why its doing it, reading the string in and making the replacement
but then reading the string in for the next pattern, but i don't want it to
do this. How do i stop preg_replace from reading in the same part of the
string that has been replaced already?
(it's a bad day! :( )
.
Relevant Pages
- Re: [PHP] preg_replace strange behaviour, duplicates
... Problem i am having is the replacement takes place then it seems to do it ... The new Intel Core 2 Processor Technology Duo Processor Technology T8300 ... I can see why its doing it, reading the string in and making the ... (php.general) - [PHP] preg_replace strange behaviour, duplicates
... The new Intel Core 2 Processor Technology Duo Processor Technology T8300 ... I can see why its doing it, reading the string in and making the replacement ... (php.general) - Re: Replace strings in a text file and get the number of replacementsmade
... It might be faster to use sb.Replace(search, replacement, i, search.Length) ... If you simply call StringBuilder.Replace(string, string, int, int) instead of having your own AreEqualmethod followed by a call to Removeand Insert, the performance should be practically identical, but you wouldn't get any information about how many replacements occurred. ... You could do a little hack by searching for the first character that differs between the search and replacement strings, and then bumping a counter after each call to StringBuilder.Replacebased on whether the character at the same offset within the current StringBuilder has changed. ... (microsoft.public.dotnet.languages.csharp) - Re: Replace strings in a text file and get the number ofreplacementsmade
... I haven't tried, but I assume Regex would find 2 matches, while replace will replace just once the pattern. ... The regex will also be expensive, because it will have to be parsed/compiled every time a new pattern is used (and if it is a user defined replacement, that would be more often than not). ... I'd opt for an extention method on string and write an efficient version of a Replace method that returns the number of matches. ... If you simply call StringBuilder.Replace(string, string, int, int) ... (microsoft.public.dotnet.languages.csharp) - Re: replacing text in textfile
... ReplaceFile does indeed creat the replacement file - but nothing is in it. ... Public Function ReplaceInFile(InputFile As String, ... Dim Fnum As Integer ' get a FreeFile number ... Dim FileLength As Long 'Just in case its really big ... (microsoft.public.access.modulesdaovba) |
|