Thursday, June 30, 2011

PHP regex to match any link on text

//1st match subdomain: xyz. OR
// next, match domain
// then match .
// then match dual extension: .com or .org or ...and .sg OR
// then match single extension: TLD: .com or .org or .net or .... OR
// then match single country name .us
// then match uri, example: /xxx
// also try to match ending /
// and also try to match with ending name /xxx/lastnamehere
$sSubDomainSet = "[^.,\:\+\s]*\."; // xyz.
$sPatSubDomain = "(" . $sSubDomainSet . ")*";
$sPatCountryDomain = "((com|org|net|name|mil|gov|co|info)\.[a-z]{2})";
$sPatTLD = "(com|org|net|name|mil|gov|info)"; //top level domain tld
$sPatCountryTLD = "([a-z]{2})";
$sURIChar = "[a-z0-9\.\?\=\&\(\&amp\;)\-\_\+\%]";
$sPatURI = "([\/]*" . $sURIChar . "*)?";
$sPatURISlash = "([\/])?";
$sPatLastName = "(" . $sURIChar . "*)";
//match last uri name, example domain.com/email/xxx (match xxx)
$sPattern = "/(" . "((http:\/\/)|(https:\/\/))?" . $sPatSubDomain . $sSubDomainSet .
"(" . $sPatCountryDomain . "|" . $sPatTLD . "|" . $sPatCountryTLD . ")" .
$sPatURI . $sPatURISlash . $sPatLastName .
")/is";
preg_match_all($sPattern, $sContent, $matches, PREG_PATTERN_ORDER);
if (count($matches[1]) > 0) {
$sResult = $matches[1][0];//match first title tag
}

Example Results:
Original: this is a link https://www.yahoo.com/, result: https://www.yahoo.com/
Original: this is a link https://yahoo.com, result: https://yahoo.com
Original: this is a link http://yahoo.com, result: http://yahoo.com
Original: this is a link www.yahoo.com, result: www.yahoo.com
Original: this is a link yahoo.com, result: yahoo.com
Original: this is a link www.yahoo.org, result: www.yahoo.org
Original: this is a link www.whitehouse.gov, result: www.whitehouse.gov
Original: this is a link www.whitehouse.mil, result: www.whitehouse.mil
Original: this is a link www.yahoo.net, result: www.yahoo.net
Original: this is a link yahoo.com.my, result: yahoo.com.my
Original: this is a link yahoo.org.my, result: yahoo.org.my
Original: this is a link yahoo.co.sg, result: yahoo.co.sg
Original: this is a link yahoo.co, result: yahoo.co
Original: this is a link yahoo.my, result: yahoo.my
Original: this is a link yahoo.us, result: yahoo.us
Original: this is a link xyz.123.www.yahoo.com, result: xyz.123.www.yahoo.com
Original: this is a link yahoo.com/news is this right?, result: yahoo.com/news
Original: this is a link yahoo.com/ i think so, result: yahoo.com/
Original: this is a link yahoo.com/aboutus.php, result: yahoo.com/aboutus.php
Original: this is a link yahoo.com/email/ maybe its right, result: yahoo.com/email/
Original: this is a link yahoo.com/email/compose,gotta love it, result: yahoo.com/email/compose
Original: this is a link yahoo.COM/email/compose.html?search=233 haha, result: yahoo.COM/email/compose.html?search=233
Original: this is a link yahoo.com/email/compose.html?search=233&xx=22 haha, result: yahoo.com/email/compose.html?search=233&xx=22
Original: this is a link yahoo.com/email/compose.html?search=233&uu=232 hehe, result: yahoo.com/email/compose.html?search=233&uu=232
Original: this is a link yahoo.com/email/compose.html?search=233&xx=2?ehh, result: yahoo.com/email/compose.html?search=233&xx=2?ehh
Original: this is a link yahoo.com/email/com-pose.html, result: yahoo.com/email/com-pose.html
Original: this is a link yahoo.com/email/com-pose_.html?a=1, result: yahoo.com/email/com-pose_.html?a=1
Original: this is a link yahoo.com/email/com+20%pose.html?a=1, result: yahoo.com/email/com+20%pose.html?a=1


p/s: yes i know the line with double ? is incorrect, im lazy to write it :)

Full pattern string:
/(((http:\/\/)|(https:\/\/))?([^.,\:\+\s]*\.)*[^.,\:\+\s]*\.(((com|org|net|name|mil|gov|co|info)\.[a-z]{2})|(com|org|net|name|mil|gov|info)|([a-z]{2}))([\/]*[a-z0-9\.\?\=\&\(\&amp\;)\-\_\+\%]*)?([\/])?([a-z0-9\.\?\=\&\(\&amp\;)\-\_\+\%]*))/is

updated aug 28, avoid +,: in name, added http and https support

Wednesday, June 8, 2011

Ubuntu 11.04 / Mint 11 black screen or blank screen?

It caught me by surprise when i used the nvidia driver with 3d experimental driver.
The problem lies on 2 issue:
1. new kernel mode auto enable high resolution via vga=758, which isnt supported on my asus laptop
2. the xwindows nvidia experimental driver didnt work.
So i got a black screen (not even backlight).

to fix it....
@ the grup screen, key edit (e) to edit on recovery mode
go to the line /boot linux ... (add "nomodeset" at the end)

This shall disable high resolution screen for boot up kernel.
in recovery mode, you may reconfigure your graphic to basic graphic,
then relaunch x windows. Inside there, click on additional driver, and disable the experimental driver.
Finally, reboot ;)

Wednesday, May 25, 2011

MODx Revo TinyMCE font color, table button

Default MODx Revolution TinyMCE doesnt contain font colour or table button.
To add font color, go to system : system setting
search for filter: "tiny"
add "forecolor" into tiny.custom_buttons# key.
add "table" into tiny.custom_buttons# key.

then add "table" into tiny.custom_plugins

This shall do a quick fix to have the things you need.
But do look for tiny.css_selectors to do a proper font face and size via classes.

For other buttons and plugins:

Monday, May 23, 2011

dhtmlx combo forceselection

Hi found a way to hack on dhtmlx combo to allow forceselection.
just add this code after dhtmlxcombo.js

dhtmlXCombo.prototype._confirmSelection = function(data,status){
if(arguments.length==0){
var z=this.getOptionByLabel(this.DOMelem_input.value);
//data = z?z.value:this.DOMelem_input.value;
data = z?z.value:null;
status = (z==null);
if (data==this.getActualValue()) return;
};
this.DOMelem_hidden_input.value=data;
this.DOMelem_hidden_input2.value = (status?"true":"false");
this.callEvent("onChange",[]);
this._activeMode=false;
}

Sunday, May 22, 2011

jQuery idTabs switching tab

idTabs contains a bug where you may not program it to switch tab by passing

jQuery("#mytab.idTabs").idTabs(0);

This is due to the if statement in idTabs function which collect the "selected" class in a tag to determine the active tab.

the fixed version is available here:

Friday, May 20, 2011

HTML5 framework

52 Framework is pretty much a all in one packed framework for any html5 start off.
It contains grid with columns CSS, CSS 3 , HTML5, Form CSS, Form Validation and CSS reset.
It also include modernizer to fix up some issue with IE.

For those whom need IE canvas on IE 8 or below:

Sunday, May 8, 2011

div offsetWidth 0?

Did you ever encounter offsetWidth returning 0?
In my case, it happen when ive a div within a div where it represent a container for a jquery tab.
Somehow, i found out that the offsetWidth return 0 if i did not tab to that tab.
Once ive tab into that tab, the offsetWidth now return the actual width rendered.
Took me some hours to solve this issue...