Asked  7 Months ago    Answers:  5   Viewed   34 times

I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.

function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

$stringToArray = explode(" ",$_POST['text']);

  foreach($stringToArray as $key=>$val){
  $urlvalid = isValidURL($val);
  if($urlvalid){
  $_SESSION["messages"][] = "NO URLS ALLOWED!";
  header("Location: http://www.domain.com/post/id/".$_POST['postID']);
     exit();
     }
     }

Thanks! Stefan

 Answers

65

You want something like:

%^((https?://)|(www.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i

this is using the | to match either http:// or www at the beginning. I changed the delimiter to % to avoid clashing with the |

Wednesday, March 31, 2021
 
michele
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 7 Months ago
81

Try:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(w+)/(w+)$ $1/public/$2 [QSA]
</IfModule>
Wednesday, March 31, 2021
 
viper
answered 7 Months ago
73

Protocol relative URL

You may receive unusual security warnings in some browsers.

See also, Wikipedia Protocol-relative URLs for a brief definition.

At one time, it was recommended; but going forward, it should be avoided.

See also the Stack Overflow question Why use protocol-relative URLs at all?.

Thursday, June 17, 2021
 
Uours
answered 5 Months ago
31

Your expression is incorrect. Try:

preg_match('/< *img[^>]*src *= *["']?([^"']*)/i', $row->introtext, $matches);

Note the removal of brackets around img and src and some other cleanups.

Friday, July 2, 2021
 
xrock
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 :