s1w Posted April 28, 2009 Share Posted April 28, 2009 Hello. My problem is about processing Regular Expressions (Perl-Compatible) as on img: i've tried a hundreds of patterns, and still nothing for #1 not working: '/\..+$/' '/\..+?$/' '/\..+?(?!\.)$/' \ yes, this is followed char, I only dont know if logic is not inversed by $ for #2 $replace = '/\.[\w\W]+$/'; $with = ''; preg_replace($replace,$with,$str); (without returning correct alphanumeric findings for now, but still not working) others: '/\.[\w\W^\.]+$/' '/\.[\w\W^\.]+?$/' '/\.[\w\W[^\.]]+?$/' '/\.[\w[\W[^\.]]]+?$/' '/\.[\w\W&&[^\.]]]+?$/' //&& working in java, but rather not in php '/\.(?:\w|\W)+$/' //or even without [] '/\.(?:\w?|\W?)+$/' first idea with returnings: '/\s*\.[(\w)\W^\.]+?$/' '/\.(?:\(w?)|\W?)+$/' $replacer = '\.$1'; anyone could help me with this? greetings michael Quote Link to comment https://forums.phpfreaks.com/topic/156007-problem-with-inventing-pattern-for-preg_replace/ Share on other sites More sharing options...
pkSML Posted April 28, 2009 Share Posted April 28, 2009 You should be able to do that with strpos and preg_replace in PHP. <?php $pos = strrpos($str, '.'); // this will give us the position of the last dot in the string -- strpos() will give you the first occurrence $newstring = preg_replace("/[^a-zA-Z0-9]/", "", substr($str, 0, $pos)); // You'll have $newstring, which contains all alphanumeric characters from the beginning of $str until the last dot in the string ?> Quote Link to comment https://forums.phpfreaks.com/topic/156007-problem-with-inventing-pattern-for-preg_replace/#findComment-821286 Share on other sites More sharing options...
thebadbad Posted April 28, 2009 Share Posted April 28, 2009 Personally I would do it with strrpos() like pkSML suggests, but if you want the regular expressions solutions (the last one runs another preg_replace() inside the replacement): #1 <?php $str = 'file| .,, & .123^; ..asd gf@ 4& 1 *5@n% . #{}67$ n89 ,, & |'; echo preg_replace('~(.*)\..*$~is', '$1', $str); // file| .,, & .123^; ..asd gf@ 4& 1 *5@n% ?> #2 <?php $str = 'file| .,, & .123^; ..asd gf@ 4& 1 *5@n% . #{}67$ n89 ,, & |'; echo preg_replace('~(.*\.)(.*)$~ise', "'$1' . preg_replace('~[^0-9a-z]~i', '', '$2')", $str); // file| .,, & .123^; ..asd gf@ 4& 1 *5@n% .67n89 ?> Quote Link to comment https://forums.phpfreaks.com/topic/156007-problem-with-inventing-pattern-for-preg_replace/#findComment-821299 Share on other sites More sharing options...
pkSML Posted April 28, 2009 Share Posted April 28, 2009 Woops! I thought you were just discarding everything after the last dot. @thebadbad - nice to see there's another way of doing things, probably closer to 'best practice' Anyhow, here's how my code changes now that I understand what you needed to happen. <?php $pos = strrpos($str, '.'); // this will give us the position of the last dot in the string -- strpos() will give you the first occurrence $newstring = substr($str, 0, $pos) . preg_replace("/[^a-zA-Z0-9]/", "", substr($str, $pos)); // You'll have $newstring, which contains everything up to the last dot and all alphanumeric characters after the last dot in the string ?> Quote Link to comment https://forums.phpfreaks.com/topic/156007-problem-with-inventing-pattern-for-preg_replace/#findComment-821363 Share on other sites More sharing options...
s1w Posted April 29, 2009 Author Share Posted April 29, 2009 Thank You! both of You enlighten me about greatly important thing, that i couldnt find in any documentation or so much commentaries, eg main php manual http://pl2.php.net/manual/pl/function.preg-replace.php - about, that PHP can also take function as replacer! ealrier it was obvious for me it was impossible. Both solves are working well, but i was affraid that i would have problem to implement either two to my code, finally i turned up that indeed thebadbad #2 wouldnt make trouble as pattern with function stored in array. If I want pkSML version that looks v clear for me, i would have to change preg_replace construction. <?php $replace = array('/[\[\{]/', '/[\]\}]/', //change parenthesis [] {} na () '/(.*)\.(.*)$/e', //format extension '/[^\w\s\(\)\-`\.,;\+=#&!]|'. //allow some strange chars in filename _ -() `,.;+=!&# '^[\s\.,;]*[!&\-#]+[\s,;!&\-#]*/', //format tolerable beggining '/^[\s\.,;]+|\s+(?=\s.)/'); //or cut, and finally shorten multiple spaces $with = array('(',')',"trim('$1').'.'.preg_replace('~[\W]~','','$2')",'_',''); printt ('file|'.preg_replace($replace,$with,$str).'|'); //own print for debugging ?> thanks for help Quote Link to comment https://forums.phpfreaks.com/topic/156007-problem-with-inventing-pattern-for-preg_replace/#findComment-821506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.