Forum Moderators: coopster

Message Too Old, No Replies

Regex help

regex stripping x character

         

jcodemasters

12:50 am on Jan 27, 2008 (gmt 0)

10+ Year Member



Hi,
I am not a good at regex however i always try to improve it.. I have 10000s files which needs to imported in database but before they import i want to take out following things

- Numbers
- special characters (){}[]@$% etc
- patterns 1600x1200, 800x600, 1024 x 786, 1024 X 786

X can be capital or small i have written all the rules but problem with below code is its stripping X or x from the text.

$file = '3d MetarelicXx, - ([1600x1200]).JPG';
$ext = strrchr($file, '.');
$name = str_replace("3d","#d",$file);
$name = str_replace("3D","#d",$name);
$name = preg_replace('/([0-9 x 0-9])/', ' ', $name);
$name = preg_replace('/([0-9x0-9])/', ' ', $name);
$name = preg_replace('/([0-9X0-9])/', ' ', $name);
$name = preg_replace('/([0-9])/','',$name);
$name = preg_replace('/([-_%])/', ' ', $name);
$name = preg_replace('/([+(),\[\]])/', ' ', $name);
$name = trim(str_replace($ext,'',$name));
$name = str_replace('#d','3D',$name);
$name = ucwords($name);

any suggestion?

cameraman

1:09 am on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These three lines are causing the x's to go away (which you probably already knew):
$name = preg_replace('/([0-9 x 0-9])/', ' ', $name);
$name = preg_replace('/([0-9x0-9])/', ' ', $name);
$name = preg_replace('/([0-9X0-9])/', ' ', $name);

It's because you're using character classes and the x qualifies.
Try this one line instead of those three:
$name=preg_replace('/(\d+ {0,1}x {0,1}\d+)/i','',$name);

This pattern is satisfied by 1 or more digits followed by an optional space followed by an x followed by an optional space followed by 1 or more digits. The i at the end causes it to be case insensitive.

jcodemasters

3:30 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



thanks, its working.. whats the purpose of \d

mehh

4:30 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



\d represents any decimal digit.
Preg Pattern Reference [uk2.php.net] is your friend!

PHP_Chimp

5:29 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$name=preg_replace('/(\d+ {0,1}x {0,1}\d+)/i','',$name);

The {0,1} can be shortened to? as it means the same thing.

$name=preg_replace('/(\d+?x?\d+)/i','',$name); // where have all the spaces gone?

If you dont like regexes then I dont know what looks more voodoo...the {0,1} or?.

Although after having previewed this with the? the space disappears :( so I guess that is why the original {} is in there.
So to keep the forum happy


$name=preg_replace('/(\d+[ ]?x[ ]?\d+)/i','',$name);

More voodoo :p

mehh

6:34 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



it may be best to replace the space with \s, also the brackets arn't doing anything at the moment so I would remove them to get somthing like:
$name=preg_replace('/\d+\s?x\s?\d+/i','',$name);

jcodemasters

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

10+ Year Member



thanks my friends!