3

just need to check if a given string is an URL or something else. Can I use something like the following or Jomlla already has something to deal with it:

$url = "http://www.my_url.com";
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
    // is URL
} else {
    // not URL
}

Thank you.

3

The best solution would be to use preg_match() with a regular expression. One I've used before is:

((mailto\:|(news|(ht|f)tp(s?))|telnet|webcal|rss|nntp|file\://){1}\S+)

You can also search and find many other variations that are suited for specific tasks.

  • Escaping : with a backslash is not needed, nor is {1}, nor is wrapping the entire pattern in parentheses (unless you mean to use them as pattern delimiters). This pattern doesn't include any anchor metacharacters (^ or $) so My company's name is rssi will be matched, though it is not a url. – mickmackusa Oct 6 '18 at 22:25
2

Joomla has no 'url' filter defined, you can check available filters at this url Secure coding guidelines

Also, you can extend JInput class if you want, but I don't think it's worth.

For permanent override of JInput or JFilterInput, you must write a plugin, here is a example: Override Core Joomla! Classes

1

Admittedly filter_var($url, FILTER_VALIDATE_URL) is imperfect, but regex is likely to be more so (and as the accuracy of the regex pattern improves, code-bloat will swell and readability/maintainability will plummet).

As PHP continues to improve with each new version, the accuracy of filter_var() will only get better. If you roll your own static regex pattern, the only hope of improvement rests on your shoulders and relies on you dedicating time and effort into refining it.

The green tick at StackOverflow is not always to be trusted -- also pay attention to the upvotes and comments. This page is certainly worth a read: PHP validation/regex for URL

My advice (shared by many developers) is that you should only use regex when it offers a unique advantage that other simpler php functions do not. Until you encounter a case where filter_var() fails you, you should resist relying on regex for this task.

As for Joomla-centric reading...

There is no tag with an id on this page, so you'll have to scroll down to the bottom of the page: https://docs.joomla.org/Server-side_form_validation

https://docs.joomla.org/URL_form_field_type

Joomla doesn't currently offer a JFactory::getApplication->input->... validator for urls, probably because there isn't a hard-fast solution to build it upon.


p.s.

if (!filter_var($url, FILTER_VALIDATE_URL) === false) {

is the same as

if (filter_var($url, FILTER_VALIDATE_URL) !== false) {

is the same as

if (filter_var($url, FILTER_VALIDATE_URL)) { <-- just use this one for brevity



p.p.s. If you really need to verify that a url is real/live, then you can set up a separate call to the url and check for a "heartbeat", but there is a fair chance that that is overkill for most projects.

-1

I solve this needs some time ago with this solution:

<?php
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
       if (strpos($url,'blog') !== false) {
          echo '<!-- Abinhas coloridas na Categoria -->';
       }else{
          echo '<!-- Abinhas PB na Categoria -->';
       }
?>

If your content was in "module" like custom html, you can try the Advanced Module Pro from "Regular Labs (no numbers)". You can activate "URL filter": https://www.regularlabs.com/extensions/advancedmodulemanager#tutorial

I hope this helps.

  • 1
    You code does not check if the given string is a URL, but instead finds the position of the first occurance of a substring. In your case....blog – Lodder Apr 21 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.