Reply to Re: regex/preg_replace? How do I do this?

Your name:

Reply:


Posted by Rami Elomaa on 06/04/07 14:32

chowsapal kirjoitti:
> On Jun 4, 7:58 am, GoL <usenet@^^.wwwspace.net._nospam_^^> wrote:
>> Hi all,
>>
>> I have a problem that I *think* is simple, but since I'm completely
>> ignorant about regex expression I don't have a clue how to do it...
>>
>> What I need to do is to find all instances in a string where there are
>> exactly 10 digits in a row, and replace the 4th, 5th and 6th digit
>> with a # sign.
>> So if I have the string:
>>
>> "some text here 1234567890 more text 0987654321 and more text"
>>
>> I want it replaced with:
>>
>> "some text here 123###7890 more text 098###4321 and more text"
>>
>> How do I do this?
>> Greatful for any help.
>>
>> Thanks,
>> /G
>
> GoL,
>
> You could use something like this:
>
> preg_replace('/(\d{3})\d{3}(\d{4})/', '\1###\2', '1234567890');
>
> where '1234567890' is your text. Depending on the format of your
> input source, you may also need to specify the delimiter at the
> beginning or end of the expression, but this will work for a plain
> list of the 10-digit numbers.

But it should not match to 123456789012345 (ie. >10 digits) but your
example will match it. When you delimit the beginning and end of the
number to a word boundary, you'd want something like:
/\b(\d{3})\d{3}(\d{4})\b/ right?

--
Rami.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация