Posts Tagged PHP Hacks

Make Drupal Localhost Mail Work

If you develop Drupal locally and try to use the PHP mail function, you may encounter problem. The PHP mail function cannot send mail out on your local system. To solve the problem, you can install a free local mail server on your computer, and test it locally.

First, read my another post here about how to install the local mail server.

After that, you can go to WordPress Admin area and change some of the user email to xxx@example.com, and you should be able to send mail in Drupal.

Hope this helps!

No Comments

PHP Query one time PHP Repeat MySQL Array from Same Query

If you use PHP to query MySQL database once, output the array of data and later want to retrieve the result again. One example is when you want to retrieve the list of array results ordered randomly and later when you retrieve the same list of results, you want it to be the same random list order. Like the one below, you will find that it won’t just work out of the box.

$sql = "SELECT * from table ORDER BY RAND()";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
// do stuff with $row
}

while ($row = mysql_fetch_assoc($result)) {
// do other stuff with $row
}

To make it work, you need a PHP function called mysql_data_seek, below is the way to use it

$sql = "SELECT * from table ORDER BY RAND()";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
// do stuff with $row
}

mysql_data_seek($result, 0);

while ($row = mysql_fetch_assoc($result)) {
// do other stuff with $row
}

2 Comments

PHP Regex – Finding Similar Words

I take a break from fighting evil visitors. [] is a quite useful PHP PCRE expression that helps finding similar words.

Regular Expression Explanation:

\b a word boundary, followed by …
b letter ‘b’, followed by …
[aio] one of a, i, or o, followed by …
t letter ‘t’, and finally …
\b a word boundary.


Source Code:

<html>
<head><title>Finding Similar Words</title></head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>"
method="post">
<input type="text" name="value"
value="<?php print $_POST['value'];?>" />
<br />
<input type="submit" value="Submit" />
<br /><br />
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$mystr = $_POST['value'];
if ( preg_match( "/\bb[aio]t\b/", $mystr ) ) {
echo "Yes!<br/>";
} else {
echo "Uh, no.<br/>";
}
}
?>
</form>
</body>
</html>

,

No Comments