regular expression

Find the boundary of a word that starts åäö

This is how you find a whole word by its boundary when the word is starting with åäö. The usual boundary expression can handle if Swedish characters is in the string but not if it starts (or ends) the word. That means that a word like "öknen" can't be found but a word like "behöver" will be found.

<?php
$text
= "I öknen behöver man vatten";
$keyword = "öknen";
//$keyword = "behöver";
?>

This is the usual boundary expression

How to find exact match of word in a string

This is how you find an exact word in some text string. The "\b" is for finding the words boundary and the "i" is to let the search be case insensitive.

<?php

$matches
= array();
$text = "Play poker"; //Some text
$keyword = "Poker";
   
preg_match("/\b".$keyword."\b/i", $text, $matches, PREG_OFFSET_CAPTURE);
       
foreach (
$matches as $key => $value) {
  echo
"<br>$key => $value";
}
?>


Array
(
    [0] => Array
        (
            [0] => poker
            [1] => 5
        )

)

Validate URL by regular expression

This is how you can validate an URL by examining the pattern.

<?php
function  validateURL($url) {
 
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
 return
preg_match($pattern, $url);
}
?>

Or from Drupal 6

/**
* Verify the syntax of the given URL.
*
* This function should only be used on actual URLs.

Check if URL exist

This function checks if a given URL is valid by using fsockopen.


function is_valid_url ($url){
$url = @parse_url($url);
if ( ! $url) {
return false;
}

$url = array_map('trim', $url);
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
$path = (isset($url['path'])) ? $url['path'] : '';

if ($path == ''){
$path = '/';
}

$path .= ( isset ( $url['query'] ) ) ?

Pages