/    Sign up×
Community /Pin to ProfileBookmark

Escaping a plus sign (+)

How does one escape a plus sign in PHP, or rather get it to work in the following script:

[code=php]
$who = preg_replace(‘/%20/’, ‘ ‘, $who);
$who = preg_replace(‘/-/’, ‘ ‘, $who);
$who = preg_replace(‘/+/’, ‘ ‘, $who);
[/code]

The first two expressions work fine, the last is crashing, so there must be a problem with the + being executed instead of parsed as text the way I want… I’ve tried a backslash to no effect… (I’m not very good at PHP ? )

I’m trying to capture an URL which includes plus signs:
http:// [url]www.URL[/url] . com/file.php?dog+cat+mouse

I’m stripping out the dog+cat+mouse and then stripping the plus signs, replacing them with spaces.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 22.2009 — You could use urldecode() on the string (it should take care of the "+" and any "%xx" characters), then apply any other changes you need to the result.
Copy linkTweet thisAlerts:
@donatelloauthorNov 22.2009 — You could use urldecode() on the string (it should take care of the "+" and any "%xx" characters), then apply any other changes you need to the result.[/QUOTE]

Thanks!

It works like a charm:
[code=php]
$who = urldecode($who);
$who = preg_replace('/-/', ' ', $who);
[/code]
Copy linkTweet thisAlerts:
@thraddashNov 22.2009 — You should really be using the [B]str_replace[/B] function instead, as it seems you don't need a regular expression to do the match.

[code=php]$who = str_replace('-', ' ', $who);[/code]
Copy linkTweet thisAlerts:
@donatelloauthorNov 22.2009 — You should really be using the [B]str_replace[/B] function instead, as it seems you don't need a regular expression to do the match.

[code=php]$who = str_replace('-', ' ', $who);[/code][/QUOTE]


Thanks for the tip, done!<P>

Maybe one of these days I'll know what I'm doing in PHP... ?

Can anyone explain why? I'm curious to know...

is preg_replace more taxing on Apache?

is preg_replace being deprecated?

or is it just good programming?

Thanks!
Copy linkTweet thisAlerts:
@criterion9Nov 22.2009 — str_replace has less overhead and is less taxing on the processor especially when you are just replacing a single character. More complicated replaces/searches can justify the extra overhead of the ereg/preg functions.
×

Success!

Help @donatello 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.18,
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,
)...