Jump to content


Photo

The use of preg_replace


  • Please log in to reply
4 replies to this topic

#1 dymon

dymon
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 18 October 2006 - 02:24 PM

Hi,

I have to replace some symbols with empty string, for example:

'\abd' to replace with '', I need to replace the back slash and 2 or 3 symbols after it with '', and no matter what the 2 or 3 symbols are.

I know that for this I need the function preg_replace but I can not make it work.

Thanks in advance.

Dymon
Web is the future ... desktop will die ...

#2 effigy

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

Posted 18 October 2006 - 02:29 PM

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

#3 dymon

dymon
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 18 October 2006 - 02:40 PM

What I have tried is not really what I need :). As I write I don't understand how to use it correctly.

$formated = preg_replace(array('/\w+/'),'', $formated);

But this replace everything.
Web is the future ... desktop will die ...

#4 effigy

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

Posted 18 October 2006 - 03:03 PM

Try this:

<pre>
<?php

	$tests = array(
		'\abd',
		'abd',
		'xx\abdxx',
		'xx\abxx',
		'xx\axx',
		'x\ax',
		'x\a',
	);
	
	foreach ($tests as $test) {
		echo "$test => ";
		echo preg_replace('/\\\(?:\w){2,3}/', '', $test), '<br/>';
	}

?> 
</pre>


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

#5 dymon

dymon
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 18 October 2006 - 03:09 PM

Thanks, it works fine. :)
Web is the future ... desktop will die ...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users