n00b PHP question

edited April 2004 in Internet & Media
I'm looking at the following code:

<?php
[php]
<?php
// En: Begin PHP Code / Fr: Debut code PHP
/******************************************************************************\
* Random Image Displayer Version 1.0 *
* Copyright 2000 Frederic TYNDIUK (FTLS) All Rights Reserved. *
* E-Mail: tyndiuk@ftls.org Script License: GPL *
* Created 02/28/2000 Last Modified 02/28/2000 *
* Scripts Archive at: http://www.ftls.org/php/ *
*******************************************************************************/
/*******************************************************************************/
// Necessary Variables:

$RANDOM_IMG_FILE = "list_img.txt";
// En: Absolute path and name to file contain image URL location.
// Fr: Chemin absolu (complet) et Nom du fichier contenat les URL des images.

// End Necessary Variables section
/******************************************************************************/

srand((double)microtime()*1000000);

if (file_exists($RANDOM_IMG_FILE)) {
$arry = file($RANDOM_IMG_FILE);
// En: load file.
// Fr: charge le fichier.

for($i = 0; $i < sizeof($arry) ; $i++) {
if (preg_match("/\w+/", $arry[$i]))
$good_arry[$j++] = chop($arry[$i]);
# PHP 4.0 arry_push ($good_arry, $arry[$i]);
}

$randval = rand(0, sizeof($good_arry) -1);
$html_result = "www.short-media.com";
} else {
$html_result = "error: can't open $RANDOM_IMG_FILE file";
}
// En: End PHP Code
// Fr: Fin code PHP
?>
[/php]

Everything up to the loop makes sense. And the loop in itself makes sense i'm not sure what the preg_match does. I know it's kind of like substring and looked it up on php.net. From what I gather it searches for any character on a keyboard in the string it reads? and if that's the case, what are the /'s for?

Basically can someone explain how the loop works to me?

Comments

  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited April 2004
    preg_match is a regular expression match. In this case, it's looking for a pattern in the array. If I remember my regular expressions correctly, it's looking for all alphanumeric characters one or more times. The /'s mark the beginning and end of the expression.

    Once it gets to the end of the filename it chops off any white space. It then picks a random picture by generating a random number and pulling that picture from the array, then outputs the HTML.
  • edited April 2004
    Ahhhh ok, that makes sense. Thanks a lot! :thumbsup:
  • a2jfreaka2jfreak Houston, TX Member
    edited April 2004
    I believe it's the same syntax (or very similar) to Perl RegExps (maybe that's where the p comes from in preg). Look up help for Perl Regular Expressions and you can probably find all the information you'd ever need for regular expressions
Sign In or Register to comment.