Home  |  Linux  | Mysql  | PHP  | XML
From:Nathan Wallace Date:Sun Aug  8 00:22:39 1999
Subject:PHP Knowledge Base Update -- August 7th, 1999

------------------------------------------------------------
How can I turn a string into an array?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/551
------------------------------------------------------------
James Reed

A string ($foo) is already an array. You can do something like this to
walk across the string like an array:

The only difference is that instead of using count() to find the number
of elements in this "array", you use strlen().

    <?
    $foo = "abcdefghi";

    for ($i=0; $i<strlen($foo); $i++) {
        echo $i, " -- ", $foo[$i], "<br>";
    }
    ?>


------------------------------------------------------------
How can I build a search engine for my site using PHP?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/552
------------------------------------------------------------
Mike Robinson

Why not have a look at htDig instead.

    http://www.htdig.org

Then there's this article 

    http://www.devshed.com/Server_Side/PHP/Search_Engine/

which professes to be a quick and dirty php/mysql search scheme, might
be a good place to start.


------------------------------------------------------------
How can I get all words from a sentence into an array?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/553
------------------------------------------------------------
Rasmus Lerdorf, Steve Edberg

How about:

    $regs = split("[^[:alpha:]]+",$terms);

And, if you want to use this to strip out punctuation from a sentence 
but leave contractions, etc in (eg; it's, doesn't, etc):

    $regs = split("[^[:alpha:]']+", $terms);


** OTHER RANDOMLY SELECTED PHPKB ENTRIES **


------------------------------------------------------------
Why does my content in angle brackets disappear?
How can I escape HTML so it will be displayed to the user by the browser?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/427
------------------------------------------------------------

Your content in angle brackets does not disappear or get eaten.  It's
there but your browser doesn't show it because it assumes it is a an
HTML tag even if it doesn't recognize it.

You may use either PHP's HtmlSpecialChars() or HtmlEntities() functions
to escape < > as < and > to make browsers take them literally.

In addition you may use nl2br() to convert newlines to <br> tags. 
Remember not to apply HtmlSpecialChars() after nl2br() (unless of 
course you really mean it).

    http://www.php.net/manual/function.htmlspecialchars.php3
    http://www.php.net/manual/function.nl2br.php3


------------------------------------------------------------
Do I have to escape square brackets in a regular expression?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/270
------------------------------------------------------------

The square brackets are used in a regular expression to denote a range
of possible characters.  For example:

    [gKe5] - will match any of g, K, e, 5
    [^gKe5] - will match any character other than g, K, e, 5

If you want to match a square bracket itself then you must escape it in
the regular expression.

    "\[" - will match the character [
    "\[\]" - will match the character [ then the character ]

One more example for clarity:

    "\[[gKe5][^gKe5]\]" - will match [; then one of g, K, e 5; then 
                          anything other than g, K, e, or 5; then the
                          character ]


------------------------------------------------------------
How much memory do I need to compile PHP?
Why do I get a "virtual memory exhausted" error in bison when compiling?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/381
------------------------------------------------------------

Compiling PHP requires a reasonable amount of memory.

Here is an example error message from a machine with only 8MB of RAM and
8MB of swap memory.

    ha:/tmp/php-3.0.11# make
    gcc -g -O2 -O2 -fpic  -I. -I. -I/usr/local/apache/include
    -I/tmp/include    -c language-parser.tab.c -o language-parser.tab.o
    /usr/lib/bison.simple: In function `phpparse':
    /usr/lib/bison.simple:692: virtual memory exhausted
    make: *** [language-parser.tab.o] Error 1
    ha:/tmp/php-3.0.11# 

If you are having memory problems when compiling you may like to try
installing pre-compiled PHP binaries.

--- Another suggestion -------------------------------------------------

If you are using linux, you might try something else.

Under Linux it's very easy to temporarily add some extra swapspace
using a swapfile. A swapfile is a bit slower than a swappartition,
but not much. The process described below is very similar to the
one used for swappartitions (the dd isn't needed), but a lot less
risky in case of a typo.

First you need to create the swapfile somewhere. Suppose we want
to create a 32 MB swapfile in /tmp:
  dd if=/dev/zero of=/tmp/swapfile bs=1k count=32k
  (do this as a normal user, that's much safer)

After creating the swapfile, you must use mkswap on it. If you
don't, Linux will refuse to use it as a swapfile.
  mkswap /tmp/swapfile ; sync
  (do this as a normal user)

Now the swapfile is ready to be used. Tell Linux to start using
it.
  swapon /tmp/swapfile
  (you must be root to do this)

If you use the free command, you should be able to see that the
amount of swapspace increased by almost 32 MB. (top should show
you the same.)

If you don't need the swapfile anymore, tell Linux to stop using
it and delete swapfile. Note that after a reboot Linux doesn't
automatically start using the swapfile.
  swapoff /tmp/swapfile
  (you must be root to do this)


------------------------------------------------------------
How does PHP choose integer or floating point division?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/40
------------------------------------------------------------

"/" always does floating-point division.

Actually, it tests to see if (a % b) is 0, and uses integer
division if it is 0 so an integer results. Yes, this means that
the "/" operator always does two divides.



------------------------------------------------------------
How do I get out the result of a query like "select count(*) from $table"?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/129
------------------------------------------------------------

Normally when getting data out of a table you would use code like:

$result=mysql_db_query($database,"select foo from $table");
$count = mysql_result($result,0,"foo");

Since count(*) doesn't have an accessible name (like foo) inside the
result you should use the numerical field offset.  For example:

$result=mysql_db_query($database,"select count(*) from $table");
$count = mysql_result($result,0,0);

You should be careful to check that the database returns a valid result
however, so the following code is a better example:

$result = mysql($database,"select count(*) from $table");
$count = ($result>0) ? mysql_result($result,0,0) : 0;

Using the numerical offset is much faster than specifying the column
name.  But, sometimes a column name makes coding much easier.  In these
cases you can just name the count(*) result in the database query.  The
code below gives count(*) the name "bar":

$result=mysql_db_query($database,"select count(*) as bar from $table");
$count = mysql_result($result,0,"bar");

http://www.php.net/manual/function.mysql-result.php3

An alternative to using mysql_result is to get the whole row at once as
an array with:

list($count)=mysql_fetch_row($result);

Don't be tempted to use mysql_num_rows(), using count(*) is a lot
faster.  If you use "select * from table" and then mysql_num_rows() you
ask the SQL server to return ALL entries (could be millions).  Count(*)
just returns the number of elements.




Navigate in group php.kb at sever news.php.net
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by Zareef Ahmed
Powered By PHP Consultants