/    Sign up×
Community /Pin to ProfileBookmark

Removing backslashes with regex

Hi, can anyone help me with a regex problem? I’m trying to use it to remove certain expressions from an email field in a mailform, and although it works great for almost everything it won’t remove anything that has a backslash – n, r, etc. This is what I’ve been using:

[code=php]preg_replace(“/[n|r|t]/”,” “,”$string”)[/code]

I’ve tried any number of different kinds of syntax and also str_replace and ereg_replace and none of these will remove any of those expressions. All the sites I’ve looked at and other forums I’ve asked have said this should work, but it doesn’t.

Curiously, after many hours of googling, I found ONE site which said that the regular expression should have three backslashes – [] – and when I tried it it did indeed seem to remove the backslash from the string. However if I change it to this – [n] – or this – []n – it breaks again, whereas for example – [h] – still works (and removes h).

Many thanks to anyone can shed any light on this!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@andre4s_yNov 09.2008 — So you want to match character [B][/B] + character [B]n[/B] or character [B]r[/B] or character [B]t[/B].

Here is the pattern :

<i>
</i>"/[\][n|t|r]/"

or
<i>
</i>"/[\\][ntr]/"

If you want only to match character [B][/B]

The pattern is :

<i>
</i>"/[\]/"

or this
<i>
</i>"/\/"

warning : this code will give you error
<i>
</i>"$\$"

Why??? do not look in google!! Look in PHP manual. The answer is already located exactly in there.

Hope this help you..
Copy linkTweet thisAlerts:
@andre4s_yNov 09.2008 — Review from what you have done :

pattern : [B][n][/B] will be the same with [B][n][/B] and [B][n][/B] and all of them without bracket square.

They will match newline character (n), but not characters and n.

pattern : [B][|n][/B] and [B][n][/B] will match character or n.

pattern : [B][]n[/B] and [B][]n[/B] and [B]n[/B] will match on character and n.

But :

pattern : [B]y[/B] and [B]y[/B] actually will match on character and y.

Why this happen? because character n (not char +n) has special meaning in regex : newline and y has not. Read more in manual (Pattern Syntax Regex Function Perl compatible)

For :

pattern : [B][h][/B] will match character or h, not and h. Even h has special meaning in regex : horizontal whitespace character.

if you want to match and h, you can use [B][]h[/B] or [B][]h[/B] or [B][h][/B] or [B][h][/B] or any of them without square bracket.

I hope this all not make you confuse.. ?

And i believe practices and examples make problem easier to understand.
Copy linkTweet thisAlerts:
@Tangled_FrogauthorNov 09.2008 — Many thanks, that was a great help! ?
×

Success!

Help @Tangled_Frog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...