JDev

Code, phpJanuary 23, 2008 3:31 pm

Here is a compact generic function by john.ellingsworth that generates a table from a PEAR DB result set. It even uses column names as HTML table column headings.

A hint: to better format a table when there is a null value in the resultset, add a ‘nbsp;’ character to a table cell before a field value.

Code, phpOctober 3, 2007 7:59 pm

According to ncyoung.com, you can use the following piece of code to convert line-breaks into the <br> tag in PHP:

 $str = preg_replace("/\n/","\n<BR>",$str); 

Another way to do it is just  use the <PRE> tag to enclose the string :

echo "<pre>$str</pre>"; 

phpSeptember 13, 2007 8:17 pm

An article showing how to sort numerically indexed and associative arrays in PHP.

Code, phpJuly 27, 2007 11:48 am

This message "Notice: Undefined index:" shows up in php pages when an element does not exist in an array, e.g.

if($arr[’$elementName’])… 

The cure is simple: put isset() around it. e.g.:

if(isset($arr[’$elementName’]))

 

Code, php, javaMay 3, 2007 6:00 pm

I always have trouble remembering the correct arguments to the substring() method in order to delete the last character in a String.

Here is how you do it in Java: 

str =  str.substring(0, str.length()-1);

A similar statement in PHP:

$str = substr($str,0, strlen($str)-1); 

 

 More Java tips at http://www.ekcsoft.com/coding/java

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, 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, php, mysqlMarch 5, 2007 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.

phpFebruary 20, 2007 8:43 pm

This excellent script can be found at http://reallyshiny.com/scripts/table-extractor.txt

Code, phpFebruary 4, 2007 9:44 pm

First, the code: 

$dom = new DomDocument();

$dom->load($url);

$root = $dom->documentElement;

Now, you can do anything you want with the $root that represents the document - use getElementsByTagName or get children using $root->childNodes.

If you try using  DOMDocument->load() statically, you will get the following error:

Non-static method DOMDocument::load() should not be called statically.

To avoid the error, you need to create an instance of the DomDocument object using the example above. 

 

 

Code, phpJanuary 15, 2007 9:11 pm

The code shows how to get through a proxy with user authentication

Lifted from http://php.net/fopen. Contributed by user rafaelbc.

<?php
function proxy_url($proxy_url)
{
  
$proxy_name = ‘127.0.0.1′;
  
$proxy_port = 4001;
  
$proxy_user = "user";    // added
  
$proxy_pass = "password";    // added
  
$proxy_cont = ‘’;

   $proxy_fp = fsockopen($proxy_name, $proxy_port);
   if (!
$proxy_fp)    {return false;}
  
fputs($proxy_fp, "GET $proxy_url HTTP/1.0\r\nHost: $proxy_name\r\n");
  
fputs($proxy_fp, "Proxy-Authorization: Basic " . base64_encode ("$proxy_user:$proxy_pass") . "\r\n\r\n");    // added
  
while(!feof($proxy_fp)) {$proxy_cont .= fread($proxy_fp,4096);}
  
fclose($proxy_fp);
  
$proxy_cont = substr($proxy_cont, strpos($proxy_cont,"\r\n\r\n")+4);
   return
$proxy_cont;
}
?>

php, mysqlOctober 21, 2006 7:31 pm

The easiest way to search for full words in mysql is to use regular expressions. According to this article you need to use these markers - [[:<:]], [[:>:]]

that stand for word boundaries.

First, build a variable from the search parameter :

$val = addslashes($_GET[’search’]);               

Construct a regular expression pattern: 

$reg =’[[:<:]]’. $val .’[[:>:]]’ ;           

Finally, build a sql statement:

Select comments from table1 where comments REGEXP ‘$reg’ 

 So far, it has worked for me.

php, mysqlSeptember 18, 2006 11:56 am

One can use the php function mysql_escape_string() to construct mysql queries that contain special characters like quotes, backslashes, etc. This function handles sinqle quotes well but does not seem to handle double quotes appropriately.

I got better results with the addslashes() function that handles both single and double quotes very well. 

Code, phpSeptember 16, 2006 3:14 pm

It took me a while to fix this problem. Nothing I did seemed to help. I put the session_start() statement at the beginning of the file, removed the whitespace before the session_start(). I still got the same error: Warning: session_start(): Cannot send session cookie - headers already sent by

Then I had another look at my code. It turned out the the opening php tag started on the second line, and the first line was empty. Once  I had removed the blank line, the error disappeared!

Also, if you have include files there should not be any blank lines after the closing php tag. This will cause the headers already sent error as well.

I have to thank the replies posted at the experts-exchange website that prompted me to see this error.