/    Sign up×
Community /Pin to ProfileBookmark

How To Auto Detect Url Format ?

Folks,

Building rough Searchengine.
Need to echo user submitted links on SERPs. Prolem is, different links will be in different php format. How to auto detect the format ?

Q1. Since my searchengine will be opening the iFrame (of user submitted links) to other users (keyword searchers). How will I know on auto which part of the url to run htmlentities() and which parts to run the urlencode() ?
I can’t be manually checking the url /link format on a million link each day that my users submit to me everyday.

I mean, if I was opening an iFrame to my own link then no problemo as I know my own link’s format:
Example:
(printf code from NogDog):

““
$url = ‘http://localhost/test/pagination.php’;
$search = $domain;
$tbl = ‘linking_histories’;
$col= ‘domain’;
$i = 1;
$limit = 2;
printf(
“<iframe src=’%s?mysql_tbl=%s&mysql_column=%s&keyword_search=%s&result_limit_per_page=%d&page_number=%d’></iframe><br>”,
htmlentities($url),
urlencode($tbl),
urlencode($col),
urlencode($search),
$limit, // %d place-holder will force integer
$i, // ditto
urlencode($limit),
urlencode($i)
);
““

I mean, a user might submit a normal static link like so:

A.

““
‘http://localhost/test/pagination.php’;
““

Or, a dynamic one, like these:

B.

““
‘http://localhost/test/pagination.php?keyword=cars’; //%s (printf).
““

C.

““
‘http://localhost/test/pagination.php?page=4’; //%d (printf).
““

D.

““
‘http://localhost/test/pagination.php?keyword=cars&page=4’; //%s (printf) & %d (printf).
““

For the example A submitted link, this php code is correct to echo the url in the iFrame:

““
$url = ‘http://localhost/test/pagination.php’;

printf(
“<iframe src=’%s’></iframe><br>”,
htmlentities($url),
);
““

For the example B submitted link, this particular php code is correct to echo the url in the iFrame:

““
$url = ‘http://localhost/test/pagination.php’;
$search = $domain;

printf(
“<iframe src=’%s?keyword_search=%s’></iframe><br>”,
htmlentities($url),
urlencode($search),
);
““

For the example C submitted link, this particular php code is correct to echo the url in the iFrame:

““
$url = ‘http://localhost/test/pagination.php’;
$i = 1;

printf(
“<iframe src=’%s?page_number=%d’></iframe><br>”,
htmlentities($url),
$i, // ditto
urlencode($i)
);
““

For the example D submitted link, this particular php code is correct to echo the url in the iFrame:

““
$url = ‘http://localhost/test/pagination.php’;
$search = $domain;

$i = 1;

printf(
“<iframe src=’%s?mysql_tbl=%s&mysql_column=%s&keyword_search=%s&page_number=%d’></iframe><br>”,
htmlentities($url),
urlencode($search),
$i, // ditto
urlencode($i)
);
““

As you can see from above, the 4 links on the 4 iframes are not same url format.
One uses just htmlentities() and no urlencode(),
another uses the htmentities() plus one urlencode() ONLY
while another uses the htmlentities() and TWO urlencode()
and so on.
Some links have INT while others don’t.
Now since each user submitted link will be different to each other, then I can’t use one set of printf to echo all url formats.
So how to detect the url format on auto to generate the right printf with the right data type on the printf (eg. ‘%s’, ‘%d”) for that particular url the user submits ?
Is there any function in php that can detect the url type to tell me which functions (htmlentities, urlencode(), %s, %d, etc. to use on which part of the url ? You know the var_dump() tells you the data type. Something like that I am looking for.

@NogDog

Q3.
What’s ‘ditto’ that you put on following ?

““
$i, // ditto.
““

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 19.2020 — > @developer_web#1625205 What's 'ditto' that you put on following ?

It means "same as above [comment]"

Anyway, you can use [parse_url()](https://www.php.net/manual/en/function.parse-url.php) to break up a URL into its component parts and go from there, perhaps.
Copy linkTweet thisAlerts:
@developer_webauthorNov 19.2020 — @NogDog#1625207

Do I use htmentities() or urlencode() here ?

Or both ?

Imagine url is either this:

$url = 'http://localhost/test/pagination.php?tbl=links&col=domain&search=nogdog.com&page=1&limit=5';

Or, this:

$url = http://www.nogdog.com/contactus.php;

Example 1:

printf("<iframe src='%s'></iframe><br>",
htmlentities($url));


Example 2:

printf("<iframe src='%s'></iframe><br>",
urlencode($url));


Example 3:

printf("<iframe src='%s?tbl=%s&col=%s&search=%s&limit=%d&page=%d'></iframe><br>",
htmlentities($url),
urlencode($url));


I going for EXAMPLE 3, what you say ?
Copy linkTweet thisAlerts:
@developer_webauthorDec 01.2020 — @NogDog

Do you mind replying to my previous post ?
Copy linkTweet thisAlerts:
@developer_webauthorDec 03.2020 — @Sempervivum

Care to chime in ?

Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorAug 07.2021 — @sempervivum

Do you mind answering my previous post and then close this thread ? I really need to get my head around echoing user submitted links. That means familiar myself with urlencode(), rawurlencode(), htmlentities() and htmlspecialchars().
×

Success!

Help @developer_web 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 4.26,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...