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). Link to comment https://forums.phpfreaks.com/topic/20505-removing-a-text-delimiter-like/ 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? Link to comment https://forums.phpfreaks.com/topic/20505-removing-a-text-delimiter-like/#findComment-90398 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] Link to comment https://forums.phpfreaks.com/topic/20505-removing-a-text-delimiter-like/#findComment-90400 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. Link to comment https://forums.phpfreaks.com/topic/20505-removing-a-text-delimiter-like/#findComment-90419 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. Link to comment https://forums.phpfreaks.com/topic/20505-removing-a-text-delimiter-like/#findComment-90446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.