Jump to content

Archived

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

dymon

The use of preg_replace

Recommended Posts

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

Share this post


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

Share this post


Link to post
Share on other sites
Try this:

[code]
<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>

[/code]

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.