Hi, I have tried unsuccessfully to create a function that will strip the forward slashes ("/") and periods (".") from a string. Could someone please help me out with the correct way to do it?
Thanks,
-Jeff
| SitePoint Sponsor |
Hi, I have tried unsuccessfully to create a function that will strip the forward slashes ("/") and periods (".") from a string. Could someone please help me out with the correct way to do it?
Thanks,
-Jeff
Here is a simple function:
SeanPHP Code:<?php
function seanstrip($string) {
$string = str_replace(".", "", $string);
$string = str_replace("/", "", $string);
return $string;
}
$strip = seanstrip("This is a full stop. and this is a forward slash /");
echo $strip;
?>![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
Hey Sean, thanks for your help
-Jeff





Here's a shorter version:
You need to escape the dot character and the backslash(. = any character, \. = just a dot).Code:<?php function paulstrip($string) { return preg_replace("/\.|\//", '', $string); } ?>
Last edited by Paul S; Oct 6, 2001 at 15:42.
I am going to have to pull rank (as a mentor)
1) No you don't need to escape the dot
2) There will be no paulstrip functions around here, they will be seanstrip or nothing!
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature





mm, yes you have. Check the examples hereOriginally posted by seanf
I am going to have to pull rank (as a mentor)
1) No you don't need to escape the dot
NOOO!!!!!
2) There will be no paulstrip functions around here, they will be seanstrip or nothing!
![]()
Please try this:Originally posted by Paul S
mm, yes you have. Check the examples here
This is because str_replace is matching \. not .Code:<?php $string = "This wont be replaced >. but this will >\."; $string = str_replace("\.", "", $string); echo $string; ?>
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature





Wops!!, I was thinking all the time in regular expressions![]()
[edit]Function above fixed
i find it's faster (literally) to do it like Paul's code - looking through the string once is faster than looking through it twice w/ str_replace(). actually, if you want to get down to the details (or something...), this would be the best:
it's better to use a character class than alternation w/ a pipe.PHP Code:$txt = preg_replace('#[./]+#', '', $txt);
- Matt
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
"Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR
Bookmarks