kalyan02 Posted May 9, 2007 Share Posted May 9, 2007 i would like to convert $teststring = "SESSION[logged=admin]&(GET[id=23]|VAR[page=admin]|POST[pageid!=23])"; into something like this using regex.. "$_SESSION[logged]==admin and ( $_GET[id]==23 or $page=='admin' or $_POST[pageid]!=23 )"; please let me know how to do this....as i am not able to figure it out any help regarding this is appreciated Link to comment https://forums.phpfreaks.com/topic/50645-how-to-convert-into-php-format-using-regex/ Share on other sites More sharing options...
MadTechie Posted May 9, 2007 Share Posted May 9, 2007 probably like this $test = "SESSION[logged='admin']&(GET[id=23]|VAR[page='admin']|POST[pageid!=23])"; $result = preg_replace('/(SESSION|GET|POST)/i', '$_$1', $test); Link to comment https://forums.phpfreaks.com/topic/50645-how-to-convert-into-php-format-using-regex/#findComment-248933 Share on other sites More sharing options...
obsidian Posted May 9, 2007 Share Posted May 9, 2007 probably like this $result = preg_replace('/(SESSION|GET|POST)/i', '$_$1', $subject); There's a little more to it than that, though, because of the comparison differences, too. Link to comment https://forums.phpfreaks.com/topic/50645-how-to-convert-into-php-format-using-regex/#findComment-248934 Share on other sites More sharing options...
kalyan02 Posted May 9, 2007 Author Share Posted May 9, 2007 but you expression only adds the '$' sign Link to comment https://forums.phpfreaks.com/topic/50645-how-to-convert-into-php-format-using-regex/#findComment-248941 Share on other sites More sharing options...
obsidian Posted May 9, 2007 Share Posted May 9, 2007 Try this, but it may still need some work: <?php $string = "SESSION[logged='admin']&(GET[id=23]|VAR[page='admin']|POST[pageid!=23])"; $pattern = array( '/(SESSION|GET|POST)\[([^=!\s]+)\s*([=!]+)\s*([^]]+)\]/i', '/VAR\[([^=!\s]+)\s*([=!]+)\s*([^]]+)\]/i', '/\s(\=)\s/', '/([&|]{1})/'); $replace = array( '\$_$1[$2] $3 $4', '\$$1 $2 $3', ' == ', ' $1$1 '); echo preg_replace($pattern, $replace, $string); ?> Hope this helps. Link to comment https://forums.phpfreaks.com/topic/50645-how-to-convert-into-php-format-using-regex/#findComment-248957 Share on other sites More sharing options...
kalyan02 Posted May 9, 2007 Author Share Posted May 9, 2007 it works perfectly thanks a lot Link to comment https://forums.phpfreaks.com/topic/50645-how-to-convert-into-php-format-using-regex/#findComment-248959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.