Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Can somebody please explain this code for me in simple steps?

$text = 'we need to send products ACE66765, ACE33453, and ace72345
         back to the customer.';

$regex = '/ACE\d{4}/i';

$productNumbers = getPartsWithRegex($text, $regex);

foreach($productNumbers as $productNumber){

    echo $productNumber . '<br/>';
}


function getPartsWithRegex($text, $regex){

    preg_match_all($regex, $text, $array);

    return $array[0];

}
share|improve this question

closed as off topic by palacsint, Peter Kiss, Quentin Pradet, Winston Ewert Feb 9 '13 at 22:04

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
See the FAQ, asking for code explanation is off-topic here. –  Winston Ewert Feb 9 '13 at 22:04

1 Answer 1

$text = 'we need to send products ACE66765, ACE33453, and ace72345 back to the customer.';

Set the variable $text to the string.

$regex = '/ACE\d{4}/i';

Defines the Rex-Ex(http://en.wikipedia.org/wiki/Regular_expression) searchpattern to look for words starting with ACE and having a min. of 4 numbers

$productNumbers = getPartsWithRegex($text, $regex);

Sets the variable $productNumbers as an array to the returned value of the function getPartsWithRegex. The arguments of the function are the previous two variables defined. $text and $regex.

foreach($productNumbers as $productNumber){
    echo $productNumber . '<br/>';
}

Loops through the array $productNumbers - as $productNumber. $productnumber is the value of the field. Each time printing the $productNumber followed by
- HTML newline.

function getPartsWithRegex($text, $regex) {
    preg_match_all($regex, $text, $array);
    return $array[0];
}

Defines the function called earlier, to call preg_match_all. Defines a table with all found matches. (Third argument $array).

share|improve this answer
    
thank u very much , now i get them all, i am confused with the last part which is return $array[0] in the function getPartsWithRegex() , –  Abdul Raziq Feb 10 '13 at 9:23
    
Look up preg_match_all on php.net. It returns a multidimensional array. –  Frederik Spang Feb 15 '13 at 11:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.