Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 12 September 2006 - 01:31 PM

<><><>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).
Legend has it that reading the manual never killed anyone.
My site

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 September 2006 - 01:54 PM

hmm... maybe this:
<?php
$String = preg_replace('|<><><>[0-9]{1,4}<><><>|', '', $String);
?>

you did say exactly like that, right?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 12 September 2006 - 01:57 PM

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


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

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 12 September 2006 - 02:20 PM

Thanks, guys. I think I ought to put learning regex on my birthday wish list.
Legend has it that reading the manual never killed anyone.
My site

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 12 September 2006 - 02:51 PM

Although there are many tutorials on the web, I think Mastering Regular Expressions is the best investment you can make.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users