Forum Moderators: coopster

Message Too Old, No Replies

help with preg replace

Limiting the number of characters

         

s31s3r

3:01 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Hi to all.
i need help with this code.
How do i replace "&(between 4 to 6 characters);" with "_" using preg_replace()?
Iīm using
preg_replace('([&]+[a-zA-Z0-9]+.[;]+)', '_', $string);

but i donīt now how i can limit the number of characters. Thanks!
Iīve also tried
preg_replace('([&]+[a-zA-Z0-9]{4-6}+.[;]+)', '_', $string);

but that didnīt work

PHP_Chimp

3:34 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




preg_replace('%&.{6,8};%', '_', $input);

This should replace between 6-8 of anything (.) that are between & and ; with a single _
or if you only want to replace a-z0-9 (and _, if you use \w) then -

preg_replace('%&\w{6,8};%', '_', $input);
// or
preg_replace('%&[a-z0-9]{6,8};%i', '_', $input);

coopster

6:36 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And welcome to WebmasterWorld, s31s3r.

The curly brackets (braces) are commonly referred to as min/max quantifiers. You can read more about them on the PCRE Pattern Syntax [php.net] page.

PHP_Chimp

7:03 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Iv just noticed that I didnt read your first post properly...
as preg_replace('%&[a-z0-9]{6,8};%i', '_', $input); should be -

preg_replace('%&[a-z0-9]{4,6};%i', '_', $input);

As you wanted between 4 and 6 not between 6 and 8. Apologies for not reading your post properly.

s31s3r

11:18 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Thanks everybody!
preg_replace('%&[a-z0-9]{4,6};%i', '_', $input);
That is funny as this
preg_replace('/([&]+[a-zA-Z0-5]{4,6}+[;])/', '_', $string);
will do the same too.
thanks for the help

PHP_Chimp

6:43 pm on Jan 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are quite correct that the below are almost doing the same thing.
preg_replace('%&[a-z0-9]{4,6};%i', '_', $input);
That is funny as this
preg_replace('/([&]+[a-zA-Z0-5]{4,6}+[;])/', '_', $string);

However the [ stuff ] is a character class, so you dont need to set up a single character as a character class i.e. [&] or [;]

The + means 1 or more so your regex will match
&&&&&&&&&&&&&&&&&&&&&&&&&&this; because of the [&]+

However as you have a + after your {4,6} you may find that your regex will also match any group of 4-6 characters that is repeated i.e.
&thisthisthisthisthisthisthisthis;
I haven't tried it, to see if the match above will work but you dont need that + after the {}. As the {} give min and max...but with the + you are also saying 1 or more.

The () are for a capturing sub pattern, so in this case you are not worried about capturing anything for use afterwards. So the process will be imperceptibly sped up by not using the ().

Finally the i modified saves you typing [a-zA-Z] as it means case insensitive.

However as well as me not getting the min and max number correct in my first example I also didnt notice that you are not going 0-9. So I guess that I should look more closely ;)