How to separate a possible URI from other content in PHP?

What is the easiest and fastest way to check if a string is a single URL or TEXT (which may contain URLs)

possible scenarios:

// successful scenario
$example[] = 'http://sub-domain.my-domain.com/folder/file.php?some=param';
// successful scenario
$example[] = '/assets/scripts/jquery.min.js?v=1.4';
// successful scenario
$example[] = 'jquery.min.js';
// this scenario should fail validation
$example[] = "http://www.domain.com welcome text\n and some other http://www.domain.com";
// this scenario should fail validation
$example[] = "scriptVar=50;";

I tried using my own php functions like parse_url, filter_var, but not all of them work properly.

UPDATE 1

To make this clearer, I'm trying to separate a possible URI from script content that will be inserted as a DOM element. All URLs will display as an SRC attribute and remain as content, for example:

<script type="text/javascript" src="{$string}"></script>
<script type="text/javascript">{$string}</script>

UPDATE 2 Analyzing the possible contents, I came to the conclusion that a line containing a space character or a semicolon means that the line cannot be a URI, I believe this pattern can solve my problem:

preg_match('/[\s]|[;]/', $string);

javascript/css?

+3
3
$exampleData = Array(
    'http://sub-domain.my-domain.com/folder/file.php?some=param',
    '/assets/scripts/jquery.min.js?v=1.4',
    '<a href="/assets/scripts/jquery.min.js?v=1.4">',
    '<a href="assets/scripts/jquery.min.js?v=1.4">',
    'http://www.domain.com welcome text\n and some other http://www.domain.com',
);

foreach($exampleData as $example)
{
    echo "Trying \"" . $example . "\" -> ";

    echo (preg_match('%((http(s)?://|www\.)[^ \r\n]+|<a.+?href=(\'|")(http(s)?://|www\.|[^#])[^\4\r\n]*?\4.*?>)%i', $example)) ?
     "Match" : "No match";

    echo "\r\n";
}

:

Trying "http://sub-domain.my-domain.com/folder/file.php?some=param" -> Match
Trying "/assets/scripts/jquery.min.js?v=1.4" -> No match
Trying "<a href="/assets/scripts/jquery.min.js?v=1.4">" -> Match
Trying "<a href="assets/scripts/jquery.min.js?v=1.4">" -> Match
Trying "http://www.domain.com welcome text\n and some other http://www.domain.com" -> Match

Update:

. HTML. DOM-, :

http://simplehtmldom.sourceforge.net/

:

include_once('simple_html_dom.php');

$dom = file_get_html('http://www.stackoverflow.com/');

foreach($dom->find('script') as $scriptElement)
{
    if(strlen(trim($scriptElement->src)) > 0)
    {
        // Script with URI set
        echo "<strong>Found script with URI</strong>";
        echo "<p>" . $scriptElement->src . "</p>";
    }
    else
    {
        // Script with content
        echo "<strong>Found script with content</strong>";
        echo("<p>" . nl2br(htmlspecialchars($scriptElement->innertext)) . "</p>");
    }
}

- ( HTML):

Found script with URI
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

Found script with URI
http://sstatic.net/js/master.min.js?v=afc76d4deac3

Found script with content    
var imagePath='http://sstatic.net/stackoverflow/img/';
var inboxUnviewedCount = -1;

...etc
+2

true, URL-. , SO.

function validate_url ($url)
{
  $regex = '/^(https?|ftp):\/\/'; //protocol
  $regex .= '(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'; //username
  $regex .= '(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'; //password
  $regex .= '@)?'; //auth requires @
  $regex .= '((([a-z0-9][a-z0-9-]*[a-z0-9]\.)*'; //domain segments AND
  $regex .= '[a-z][a-z0-9-]*[a-z0-9]'; //top level domain  OR
  $regex .= '|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}';
  $regex .= '(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'; //IP address
  $regex .= ')(:\d+)?'; //port
  $regex .= ')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'; //path
  $regex .= '(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'; //query string
  $regex .= '?)?)?'; //path and query string optional
  $regex .= '(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'; //fragment
  $regex .= '$/i';

  return (preg_match($regex, $url) ? true : false);
}

: http://www.exorithm.com/algorithm/view/validate_url

, URL-, /index.php index.php

function validate_url_fragment ($url)
{
  $regex = '/^(((\/?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'; //path
  $regex .= '(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'; //query string
  $regex .= '?)?)?'; //path and query string optional
  $regex .= '(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'; //fragment
  $regex .= '$/i';

  return (preg_match($regex, $url) ? true : false);
}

if (validate_url_fragment($url) || validate_url($url)) {
  //is url
} else {
  //not url
}

( , , )

+1

filter_var , URL-:

<?php
$safe_url = filter_var( $unsafe_url, FILTER_SANITIZE_URL );
?>
0

Source: https://habr.com/ru/post/1779232/


All Articles