/    Sign up×
Community /Pin to ProfileBookmark

How to preare text to be present in a URL

Hi people,

I need to construct a URL that will contain text that users provided. Since users can provide any text, I need to clean the text such that it could be used inside a URL without making its syntax invalid.
This means some simple stuff like converting whitespaces to hyphens, but I also need to remove punctuation marks and so on.
Does anyone know of a sure way of cleaning text like that?

How can I know whether the “clean” text is safe to be used in a URL?

here is an example:
The user provided the following text: I’m Not here!
The clean version would be im-not-here
I am lowercasing the text on purpose.
I am not sure that the ! symbol can appear in a URL.

Note that the text might non-English. In such case, any non-English character must be encoded in a similar way that Wikipedia does

any tips would be appreciated
Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@tfk11Dec 25.2007 — look up the urlencode function.
Copy linkTweet thisAlerts:
@serik16Dec 27.2007 — To be sure we have a clean URL code we encode it.

If you`re making up the whole link, you have :

[COLOR="Red"]DOMAIN/[/COLOR][COLOR="Purple"]PAGE.php[/COLOR]?[COLOR="SeaGreen"]variables=something[/COLOR]

You have 2 functions for the urlencoding:

- rawurlencode($string) - this is used for the link before the "?" (with purple)

- urlencode($string) - this is used for the link after the "?" (the variables and values, with green) .

Hope this was useful.
Copy linkTweet thisAlerts:
@jasongrauthorJan 10.2008 — Thanks, that was very useful

I will be using urlencode
×

Success!

Help @jasongr 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.6,
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,
)...