Asked  7 Months ago    Answers:  5   Viewed   33 times

I've been looking for a simple regex for URLs, does anybody have one handy that works well? I didn't find one with the zend framework validation classes and have seen several implementations.

 Answers

68

I used this on a few projects, I don't believe I've run into issues, but I'm sure it's not exhaustive:

$text = preg_replace(
  '#((https?|ftp)://(S*?.S*?))([s)[]{},;"':<]|.s|$)#i',
  "'<a href="$1" target="_blank">$3</a>$4'",
  $text
);

Most of the random junk at the end is to deal with situations like http://domain.com. in a sentence (to avoid matching the trailing period). I'm sure it could be cleaned up but since it worked. I've more or less just copied it over from project to project.

Tuesday, June 1, 2021
 
Akarun
answered 7 Months ago
12

Try this:

preg_match("#b(([w-]+://?|www[.])[^s()<>]+(?:([wd]+)|([^[:punct:]s]|/)))#i", $text, $matches);

You were missing the regex delimiters (usually /, but using # here because it's more convenient for URLs)

Wednesday, March 31, 2021
 
Strae
answered 9 Months ago
79

I would not recommend to use regex, as it will be too complex and difficult to understand. Here is a PHP code to check all your constraints:

function _is_short_url($url){
        // 1. Overall URL length - May be a max of 30 charecters
        if (strlen($url) > 30) return false;

        $parts = parse_url($url);

        // No query string & no fragment
        if ($parts["query"] || $parts["fragment"]) return false;

        $path = $parts["path"];
        $pathParts = explode("/", $path);

        // 3. Number of '/' after protocol (http://) - Max 2
        if (count($pathParts) > 2) return false;

        // 2. URL length after last '/' - May be a max of 10 characters
        $lastPath = array_pop($pathParts);
        if (strlen($lastPath) > 10) return false;

        // 4. Max length of host
        if (strlen($parts["host"]) > 10) return false;

        return true;
}
Saturday, May 29, 2021
 
BetaRide
answered 7 Months ago
89

file_exists cannot be used on resources on another websites. It's intended for local filesystem. Have a look here on how to perform the check properly.

As other have mentioned in the comments and as the link says it's (probably) easiest to use get_headers function to do this:

try {
    if (strpos(get_headers($url,1),"404")!==FALSE){
        ... your code ...
    } else {
        ... you get the idea ...
    }
}
Saturday, May 29, 2021
 
Grzegorz
answered 7 Months ago
70
$sheet->setCellValueByColumnAndRow(0, 1, "2014-10-16");

Sets a string value in the cell, not a date. Just because you interpret that as a date, doesn't mean that computer programs automagically interpret it as a date.

Look at the date Examples in the PHPExcel Documentation and Examples, and you'll see that you need to set the cell value to a MS Excel serialized timestamp (a float value of the number of days since 1st January 1900). You can use the PHPExcel functions like PHPExcel_Shared_Date::PHPToExcel() to convert human dates/PHP DateTime objects/Unix timestamps to MS Excel Serialized timestamps.

$sheet->setCellValueByColumnAndRow(0, 1, PHPExcel_Shared_Date::PHPToExcel( '2014-10-16' ));

Once you've stored the value as a timestamp, you can then apply whatever date format mask you want to that cell to get your desired formatting

Wednesday, August 18, 2021
 
Fanda
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share