Tag Archives: php

php string functions





Name Description
ASCII() Return numeric value of left-most character
BIN() Return a string representation of the argument
BIT_LENGTH() Return length of argument in bits
CHAR_LENGTH() Return number of characters in argument
CHAR() Return the character for each integer passed
CHARACTER_LENGTH() A synonym for CHAR_LENGTH()
CONCAT_WS() Return concatenate with separator
CONCAT() Return concatenated string
ELT() Return string at index number
EXPORT_SET() Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string
FIELD() Return the index (position) of the first argument in the subsequent arguments
FIND_IN_SET() Return the index position of the first argument within the second argument
FORMAT() Return a number formatted to specified number of decimal places
HEX() Return a hexadecimal representation of a decimal or string value
INSERT() Insert a substring at the specified position up to the specified number of characters
INSTR() Return the index of the first occurrence of substring
LCASE() Synonym for LOWER()
LEFT() Return the leftmost number of characters as specified
LENGTH() Return the length of a string in bytes
LIKE Simple pattern matching
LOAD_FILE() Load the named file
LOCATE() Return the position of the first occurrence of substring
LOWER() Return the argument in lowercase
LPAD() Return the string argument, left-padded with the specified string
LTRIM() Remove leading spaces
MAKE_SET() Return a set of comma-separated strings that have the corresponding bit in bits set
MATCH Perform full-text search
MID() Return a substring starting from the specified position
NOT LIKE Negation of simple pattern matching
NOT REGEXP Negation of REGEXP
OCTET_LENGTH() A synonym for LENGTH()
ORD() Return character code for leftmost character of the argument
POSITION() A synonym for LOCATE()
QUOTE() Escape the argument for use in an SQL statement
REGEXP Pattern matching using regular expressions
REPEAT() Repeat a string the specified number of times
REPLACE() Replace occurrences of a specified string
REVERSE() Reverse the characters in a string
RIGHT() Return the specified rightmost number of characters
RLIKE Synonym for REGEXP
RPAD() Append string the specified number of times
RTRIM() Remove trailing spaces
SOUNDEX() Return a soundex string
SOUNDS LIKE(v4.1.0) Compare sounds
SPACE() Return a string of the specified number of spaces
STRCMP() Compare two strings
SUBSTR() Return the substring as specified
SUBSTRING_INDEX() Return a substring from a string before the specified number of occurrences of the delimiter
SUBSTRING() Return the substring as specified
TRIM() Remove leading and trailing spaces
UCASE() Synonym for UPPER()
UNHEX()(v4.1.2) Convert each pair of hexadecimal digits to a character
UPPER() Convert to uppercase

String-valued functions return NULL if the length of the result would be greater than the value of the max_allowed_packet system variable. See Section 7.5.3, “Tuning Server Parameters”.

source: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

file_get_contents vs fread

The main difference between a complete file read using fread and
file_get_contents is that file_get_contents may use mmap (if your OS supports
it) to use file memory mapping, which can eliminate a little of the copying
and/or allocation of memory in getting it from the file to PHP’s memory space





Whether it makes any difference to your system depends on your system and you
should benchmark it in relation to the rest of your script before worrying too
much about it.

Always beware of premature optimisation – you probably have larger things to
worry about first – file_get_contents() may be a slightly faster way to read
the whole file into memory, but it’s possible that it may be more efficient to
read the file in chunks and process those smaller chunks – everything depends
on context.

For reference, here are some timings from a trivial pair of scripts against a
70kB file:

Rate fread file_get_contents
fread 1562/s — -9%
file_get_contents 1724/s 10% –
source: http://bytes.com/forum/thread560079.html

Installing apache 2.2, php 5.2 and mysql 5 in windows vista

Here’s a quick guide for installing apache 2.2, php 5.2 and mysql 5 in windows vista.

Download needed files:
1. Apache – msi file
2. PHP – better choose the zip file than the msi installer
3. MYSQL – msi file
4. Create a folder on where you want your files to be placed. ie. C:\WEB

Before you proceed make you’re have turned off the UAC (User Account Control) setting  or else you’ll be on a big trouble.

A. Installing APACHE

Run the installer and just simply follow the instructions there and choose the option to run it as a service for your convenience. For the server name you can just put localhost. To make sure you’ve installed it properly open your browser and type http://localhost or http://your_computer_name and you will see a message saying IT WORKS!

B. Installing PHP

Unzip the files on the folder you’ve created on #4 which is C:\WEB. And that’s it.

C. Configuring APACHE and PHP settings

APACHE - Open httpd.conf on Apache Software Foundation/Apache2.2/conf/ and have the following settings below: TAKE NOTE OF THE FORWARD SLASH “/”.

1. Locate LoadModule and add these lines below:

  • LoadModule php5_module “C:/WEB/PHP/php5apache2_2.dll” - (Make sure you have the correct path and the name of the dll file or else you’ll encounter the error mentioned on the last part of this guide)
  • PHPIniDir “C:/WEB/PHP – (Location of your PHP mentioned on #4 )

2. Locate and uncomment the following:

  • ServerName localhost:80
  • LoadModule rewrite_module modules/mod_rewrite.so – optional, uncomment if you’ll be using mod_rewrite

3. Locate and change the path of the following lines to the folder you’ve created on #4.

  • DocumentRoot “C:/WEB”
  • <Directory “C:/WEB”>

4.  Locate and add the following

  • locate AddType and add  this line   AddType application/x-httpd-php .php
  • locate DirectoryIndex index.html  and index.php

5. Modify the following

  • Locate for <Directory “C:/WEB”> – the one you’ve just modified, the location of your files. Below it you will find Allowoverride None change it to Allowoverride All to be able to use htaccess file. - optional
  • Locate display_errors – Off  = You might want to turn this on, for debugging purposes. - optional

PHPOpen php.ini located in C:/WEB/PHP and have the following settings below: TAKE NOTE OF THE BACK SLASH “\”.

1. Locate and change the path to the folder you’ve created on #4.

  • doc_root = “C:\WEB”
  • extension_dir = “C:\WEB\PHP\ext”

2. Uncomment the following: Needed for mysql

  • extension=php_mysql.dll
  • extension=php_mysqli.dll

3. Optional. Locate and uncomment

  • short_open_tag = Off  – The default value is OFF change it to ON. You will need this if you use <? echo “hello”; ?> instead of <?php echo “hello”; ?>
  • E_ALL & ~E_NOTICE – Error reporting except for warnings and notices

D. Installing and configuring MYSQL

Just simply run the installer and follow the wizard configuration. Make sure apache is running.

Some errors you may encounter:

1. When starting the apache  you may encounter this error “...operation has failed “ – Just make sure you have the correct path for loadmodule mentioned on httpd.conf as well as other paths and if you still got the error open apache monitor click on connect and type your computer name and try starting apache again.

So that’s pretty much about it. Have fun!!!

regex

Just wanted to share regex reference site. You can check it here http://www.grymoire.com/Unix/Regular.html
for more details.

What is a Regular Expression?

A regular expression is a set of characters that specify a pattern. The term “regular” has nothing to do with a high-fiber diet. It comes from a term used to describe grammars and formal languages.

Regular expressions are used when you want to search for specify lines of text containing a particular pattern. Most of the UNIX utilities operate on ASCII files a line at a time. Regular expressions search for patterns on a single line, and not for patterns that start on one line and end on another.

Pattern Matches
^A “A” at the beginning of a line
A$ “A” at the end of a line
A^ “A^” anywhere on a line
$A “$A” anywhere on a line
^^ “^” at the beginning of a line
$$ “$” at the end of a line

You can easily search for all characters except those in square brackets by putting a “^” as the first character after the “[." To match all characters except vowels use "[^aeiou].” Like the anchors in places that can’t be considered an anchor, the characters “]” and “-” do not have a special meaning if they directly follow “[." Here are some examples:

Regular Expression Matches
[] The characters “[]“
[0] The character “0″
[0-9] Any number
[^0-9] Any character other than a number
[-0-9] Any number or a “-”
[0-9-] Any number or a “-”
[^-0-9] Any character except a number or a “-”
[]0-9] Any number or a “]”
[0-9]] Any number followed by a “]”
[0-9-z] Any number,
or any character between “9″ and “z”.
[0-9\-a\]] Any number, or
a “-”, a “a”, or a “]”

You must remember that modifiers like “*” and “\{1,5\}” only act as modifiers if they follow a character set. If they were at the beginning of a pattern, they would not be a modifier. Here is a list of examples, and the exceptions:

Regular Expression Matches
_
* Any line with an asterisk
\* Any line with an asterisk
\\ Any line with a backslash
^* Any line starting with an asterisk
^A* Any line
^A\* Any line starting with an “A*”
^AA* Any line if it starts with one “A”
^AA*B Any line with one or more “A”‘s followed by a “B”
^A\{4,8\}B Any line starting with 4, 5, 6, 7 or 8 “A”‘s
followed by a “B”
^A\{4,\}B Any line starting with 4 or more “A”‘s
followed by a “B”
^A\{4\}B Any line starting with “AAAAB”
\{4,8\} Any line with “{4,8}”
A{4,8} Any line with “A{4,8}”
Regular Expression Class Type Meaning
_
. all Character Set A single character (except newline)
^ all Anchor Beginning of line
$ all Anchor End of line
[...] all Character Set Range of characters
* all Modifier zero or more duplicates
\< Basic Anchor Beginning of word
\> Basic Anchor End of word
\(..\) Basic Backreference Remembers pattern
\1..\9 Basic Reference Recalls pattern
_+ Extended Modifier One or more duplicates
? Extended Modifier Zero or one duplicate
\{M,N\} Extended Modifier M to N Duplicates
(…|…) Extended Anchor Shows alteration
_
\(…\|…\) EMACS Anchor Shows alteration
\w EMACS Character set Matches a letter in a word
\W EMACS Character set Opposite of \w

Perl Extensions

Regular Expression
Class Type Meaning
\t Character Set tab
\n Character Set newline
\r Character Set return
\f Character Set form
\a Character Set alarm
\e Character Set escape
\033 Character Set octal
\x1B Character Set hex
\c[ Character Set control
\l Character Set lowercase
\u Character Set uppercase
\L Character Set lowercase
\U Character Set uppercase
\E Character Set end
\Q Character Set quote
\w Character Set Match a “word” character
\W Character Set Match a non-word character
\s Character Set Match a whitespace character
\S Character Set Match a non-whitespace character
\d Character Set Match a digit character
\D Character Set Match a non-digit character
\b Anchor Match a word boundary
\B Anchor Match a non-(word boundary)
\A Anchor Match only at beginning of string
\Z Anchor Match only at EOS, or before newline
\z Anchor Match only at end of string
\G Anchor Match only where previous m//g left off

reciprocal link checker script

I found this code for reciprocal link checker on http://forums.digitalpoint.com/showthread.php?t=111

maybe you want to give it a shot. gudluck :)

<?php $mydomain = "www.domain.com"; // Set this to your domain $list = file_get_contents("sites.txt"); $urls = explode ("\n", $list); ini_set (default_socket_timeout, "5"); echo "<B>Checking back links to $mydomain....</B><P><FONT SIZE=-1>"; foreach ($urls as $url) { if (strlen ($url) && $url{0} != "#") { echo $url . "<B><FONT COLOR="; if (strpos (file_get_contents($url), $mydomain) != FALSE) { echo "GREEN> Found"; } else { echo "RED> Missing"; } echo "</FONT></B><BR>"; } } echo "</FONT>"; ?>