Jump to content

Archived

This topic is now archived and is closed to further replies.

AndyB

Removing a text delimiter like <><> ...

Recommended Posts

<><><>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).

Share this post


Link to post
Share on other sites
hmm... maybe this:
[code]
<?php
$String = preg_replace('|<><><>[0-9]{1,4}<><><>|', '', $String);
?>
[/code]

you did say [b]exactly like that[/b], right?

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
Thanks, guys. I think I ought to put learning regex on my birthday wish list.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.