Page 1 of 1

Regular Expressions - String search and replace

Posted: Sun Jan 12, 2003 6:20 am
by Bucky
ok, im pretty newbish to regular expression and i'd like some help.

I want to read a string for the following characters:
< > | [ ] - + \ /

Then replace those characters with a NULL or white space.

Anyhelp?

Posted: Sun Jan 12, 2003 6:39 am
by volka
try this one

Code: Select all

<?php
$testtext = '<>|[]-+\/';
$pattern = '![<>|\[\]\-+\\\/]!';
echo preg_replace($pattern, '', $testtext);
?>

Posted: Mon Jan 13, 2003 3:42 am
by twigletmac
If you just want to get rid of those particular characters then it is not really necessary to use regular expressions, instead you could just use str_replace():

Code: Select all

$original = '<>|[]-+\/';
$from = array('<', '>', '|', '[', ']', '-', '+', '\'', '/');
$adjusted = str_replace($from, '', $original);
Mac

Posted: Mon Jan 13, 2003 4:28 am
by volka
way too simple :D

no, twigletmac is right. Although we're talking about a fraction of a second, str_replace is also faster than preg_replace in this (not only this) case ;)

Thx alot

Posted: Mon Jan 13, 2003 4:24 pm
by Bucky
Hey thx everyone, though it was an easy solution (which really made me feel like an idiot) i've bee strugling with the problem for a good 4 days. Usually im not that bad of a php guy..ahh well. Could anyone reccommend a good regular expresion tutorial? Just to find all the good stuff on it..thx in advance.

Posted: Mon Jan 13, 2003 4:47 pm
by glo-wurm
Here are a couple pretty good regex tutorial links that I had laying around...

http://japhy.perlmonk.org/book/
http://www.perldoc.com/perl5.6/pod/perlre.html

and as always, there is the PHP manual's documentation for regexes...
http://www.php.net/manual/en/pcre.pattern.syntax.php