JDev

Code, phpMarch 29, 2007 8:32 pm

This will split a string that has multiple spaces inside:

$str = " lazy fox         did   whatever      ";
$array_result  = preg_split("/[\s]+/", trim($str)); 

The resulting array contains the following tokens: lazy, fox, did, whatever. But you got to use the trim() function first, otherwise the array will contain an empty element at the end.

First I tried to use the explode() function, but it created array elements containing spaces. Preg_split() did the trick.

 

Code, phpMarch 21, 2007 6:33 pm

Here is a compact function to convert HTML color to RGB:

function html2rgb($color){   
     return array(hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)));
}

Code, jstl 1:56 pm

Here is an JSTL code example that shows how to build a select box that remembers the previous selection.

<select name=’days‘>
      <c:forTokens items="7;14;30;60;180; 365" delims=";" var="current">             
          <c:choose>
            <c:when test="${current==param.days}">
                      <OPTION selected>
                 </c:when>            
                 <c:otherwise>
                       <OPTION>
                 </c:otherwise>
             </c:choose>
               <c:out value="${current}" /></option>       
         </c:forTokens>
 </select>

Each iteration checks an option value against a request parameter that contains the value of the selected option element. If they are equal, the HTML option tag is given a selected attribute.

JSTL Books on Amazon.com 

Code, jstl 1:03 pm

Here is a simple example that shows how to populate Javascript array values from JSTL iteration tags:

<script type=’text/javascript’>
    var arr = new Array();
    <c:forEach begin="1" end="5" var="current">
        arr.push(<c:out value="${current}" />);
    </c:forEach>   
</script>

The Javascript arr array now contains values from 1 to 5.

JSTL Books on Amazon.com

Code, cssMarch 19, 2007 8:25 pm

In CSS you can also use three-character values: "06f" is equal to "0066ff".

http://www.colortools.net/article_web_colors.html

 

Code, javascript, firefox 3:24 pm

According to this article from Danny Goodman, you can’t use the .style property to determine style properties of an element if they not defined in the style attribute attached to the element (inline). If style propeties are defined internally or externally, i.e. defined by style or link tags, you have to use a window.getComputedStyle() method for Firefox or currentStyle property in IE. In this case Firefox returns font information in pixels, e.g. 14px or 18px. IE returns it as a term, e.g. large or small.

Having this information, it is possible to build a function that would increase  element font size dynamically in Firefox:

   function increaseFontSize(elem){
         var compStyle = window.getComputedStyle(elem, "");
         //For this method you have to supply style attribute information the way it is required for css.
         var size = compStyle.getPropertyValue(’font-size’);
        //remove px from the value
        size = size.replace("px", "");
        //define new larger size
        size = parseInt(size)+2;
         // set element style the Javascript way
        elem.style.fontSize = size+"px";       
  }

Code, javascriptMarch 16, 2007 7:30 pm

I had a textarea in a form that I wanted to submit on pressing the ‘Enter’ key. I have the code to do that:

http://jdev.blogsome.com/2007/03/12/capture-enter-key-on-form-submission/

 But the problem is that if you use it on a textarea, a user can’t use the Enter key to insert a line break. The solution is to find the cursor’s position and submit the form only if the cursor is at the end of the text. After some digging on the Internet I came up with this code:

function isEndPosition (tArea){

    var tval = rtrim(tArea.value);

   //Firefox
   if(tArea.selectionStart){
        var selStart = tArea.value.substr(0, tArea.selectionStart);
        return (selStart.length - tval.length >=0);        
    }
   //IE
    else {
       tArea.focus();
        len = caret(tArea);
        return (len - tval.length >=0 || len == -1)
    }
}

function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/,"");
}

The function returns true if the cursor is at the end of the text. It accepts a textarea object as a parameter. Tested in Firefox and IE 6.

The ‘caret()’ function for IE is at http://www.csie.ntu.edu.tw/~b88039/html/jslib/caret.html 

 

More JavaScript tips at http://www.ekcsoft.com/coding/js.php 

Code, phpMarch 15, 2007 11:59 am
Make everything in brackets bold
$text = "The definition of [recursion] may not be obvious."; 
echo preg_replace ('/\[([^]]+)\]/', '<b>\\1</b>', $text); 

This example produces the following result:

The definition of <b>recursion</b> may not be obvious.

Lowercase all HTML tags in the text
$html_body = "<Blockquote>A <B>rose</B> by any other name..<P></Blockquote>\n"; 
echo preg_replace ('!(</?)(\w+)([^>]*?>)!e',  
			"'\\1'.strtolower('\\2').'\\3'",   
			$html_body); 

This example produces the following output:

<blockquote>A <b>rose</b> by any other name..<p></blockquote> 

In this example, since the /e modifier is present, the captured tag name (\\2 reference) is fed through strtolower() and concatenated with the other captured pieces. Note that the backslashes in the references need to be doubled because of the double quotation marks

 

Code, javascriptMarch 12, 2007 5:07 pm

keys = {
    getCharCode: function(evt){
            return (evt.charCode) ? evt.charCode :
                ((evt.which) ? evt.which : evt.keyCode);
    },
    isEnter:function(charCode){
            return (charCode == 13);
    },

    submitViaEnter:function(evt) {
            evt = (evt) ? evt : event;
            charCode = this.getCharCode(evt);
            if (this.isEnter(charCode)) {                   

                   //replace this with your own function:
                   
form_submit();
                    return false;            
            }
            return true;
    }
}

To use it, add the following call to a text field:

onkeypress="return keys.submitViaEnter(event)"  

This code checks if a user has pressed the Enter key after finishing typing in the input field. If the key pressed is Enter, call the function that is normally invoked on the submit event. If the key pressed is not Enter, the function returns true and a user can continue typing.

Code, cssMarch 5, 2007 8:51 pm

Use the following style in order to be able to use the width attribute in Firefox: 

<style type="text/css">
span {display:-moz-inline-box; /* Gecko proprietary */
display:inline-block; /* supported by Opera and ? */
width:25%;height:1em; /* width and height required */
vertical-align:top; /* Gecko needs this */
} /* IE, Opera & Gecko slight diff */
</style>

From http://www.thescripts.com/forum/thread161350.html 

Code, php, mysql 4:54 pm

You know mysql is not enabled if you get the following error:

Fatal error: Call to undefined function: mysql_connect()

Here is an article that describes how to fix it.

Code, javascriptMarch 2, 2007 9:14 pm

abort() -    Stops the current request.

getAllResponseHeaders() -  Returns the response headers as a string.

getResponseHeader("headerLabel") - Returns a single response header as a string.

open("method", "URL"[, asyncFlag[, "userName"[, "password"]]]) - Initializes the request parameters.

send(content) - Performs the HTTP request.

setRequestHeader("label", "value") - Sets a label/value pair to the request header.

onreadystatechange - Used to set the callback function that handles request state changes.

readyState - Returns the status of the request:
        0 = uninitialized
        1 = loading
        2 = loaded
        3 = interactive
        4 = complete

responseText - Returns the server response as a string.

responseXML - Returns the server response as an XML document.

status - Returns the status code of the request.

statusText - Returns the status message of the request.