AndyB Posted September 12, 2006 Share Posted September 12, 2006 <><><>X<><><>I need to remove any string exactly like that where X can be an 1, 2, 3, or 4-digit number.If I knew what preg_replace to use .... the member I'm helping would be thrilled (I hope). Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 12, 2006 Share Posted September 12, 2006 hmm... maybe this:[code]<?php$String = preg_replace('|<><><>[0-9]{1,4}<><><>|', '', $String);?>[/code]you did say [b]exactly like that[/b], right? Quote Link to comment Share on other sites More sharing options...
effigy Posted September 12, 2006 Share Posted September 12, 2006 [code]<pre><?php $tests = array( '<><><>1<><><>', '<><><>12<><><>', '<><><>123<><><>', '<><><>1234<><><>', 'in the middle <><><>1<><><> of a string', '<><><>12345<><><>', '<><><>a<><><>', '<><>1<><>', '<>1<>', ); foreach ($tests as $test) { printf( '%-40s => %-40s<br/>', $test, preg_replace('/ ( ### Begin capturing in \1 (?:<>){3} ### 3 instances of <> ) ### End capturing (\d{1,4}) ### Capture 1 to 4 digits in \2 \1 ### Repeat match against \1 /x', '', $test) ); }?></pre>[/code][tt]<><><>1<><><> => <><><>12<><><> => <><><>123<><><> => <><><>1234<><><> => in the middle <><><>1<><><> of a string => in the middle of a string <><><>12345<><><> => <><><>12345<><><> <><><>a<><><> => <><><>a<><><> <><>1<><> => <><>1<><> <>1<> => <>1<> [/tt] Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 12, 2006 Author Share Posted September 12, 2006 Thanks, guys. I think I ought to put learning regex on my birthday wish list. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 12, 2006 Share Posted September 12, 2006 Although there are many tutorials on the web, I think [url=http://www.regex.info]Mastering Regular Expressions[/url] is the best investment you can make. Quote Link to comment 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.