[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP: Undefined offset with usort and preg_match

Posted on 2012-08-31
6
Medium Priority
?
859 Views
Last Modified: 2012-08-31
This does not work if the array contains special characters:

<pre><?php

$arr = Array ( 'xa0.txt', 'xa1.txt', 'xa10.txt', 'xa11.txt', 'xa12.txt', 'xa2.txt', 'xa3.txt', 'xa4.txt', 'xa5.txt', 'xa6.txt', 'xa7.txt', 'xa8.txt', 'xa9.txt');
usort($arr, 'cmp');
print_r($arr);

$arr = Array ( 'a/b/c_d-e555/xa0.txt', 'a/b/c_d-e555/xa1.txt', 'a/b/c_d-e555/xa10.txt',  'a/b/c_d-e555/xb10.txt', 'a/b/c_d-e555/xb11.txt', 'a/b/c_d-e555/xb2.txt', 'a/b/c_d-e555/xb1.txt', 'a/b/c_d-e555/xa11.txt', 'a/b/c_d-e555/xa12.txt', 'a/b/c_d-e555/xa2.txt', 'a/b/c_d-e555/xa3.txt', 'a/b/c_d-e555/xa4.txt', 'a/b/c_d-e555/xa5.txt', 'a/b/c_d-e555/xa6.txt', 'a/b/c_d-e555/xa7.txt', 'a/b/c_d-e555/xa8.txt', 'a/b/c_d-e555/xa9.txt');
usort($arr, 'cmp');
print_r($arr);

function cmp($a, $b) {
    $a = zeroadd($a);
    $b = zeroadd($b);
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

function zeroadd($a) {
    preg_match('/^(\w+)(\d+)(\.\w+)$/', $a, $bits);
    return sprintf("%s%05d%s",$bits[1],$bits[2],$bits[3]);
}

?></pre>

Open in new window

0
Comment
Question by:hankknight
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38353864
What do you want to sort on?
  - the entire path and file name with name adjusted to have leading zeroes; or
  - just the file name adjusted to have leading zeroes

If everything + the filename etc try the preg_match() in the zeroadd function
    preg_match('/^(.+)(\d+)(\.\w+)$/', $a, $bits);

But to depending on what you could throw at it ... the regex may need further refinement.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38353885
A slightly safer approach would to handle non-matching strings ... e.g.
function zeroadd($a) {
    if ( preg_match('/^(.+)(\d+)(\.\w+)$/', $a, $bits) )
        return sprintf("%s%05d%s",$bits[1],$bits[2],$bits[3]);
    else
        return $a;
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38354026
Please answer the question asked here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27848911.html#a38353128

What we're trying to get is a dialog that will lead us to an acceptably clear description of the input data and the outputs you want.  Computer programming only does one thing: It transforms one data set into another data set.  If we have fragments of the truth about the data, we are unlikely to give you anything more than fragments of an answer.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38354094
The output from this seems intuitively correct, but it would be better to know for sure that we had a representative test data set.

<?php // RAY_temp_hankknight.php
error_reporting(E_ALL);
echo '<pre>';

$arr = array
( 'xa0.txt'
, 'xa1.txt'
, 'xa10.txt'
, 'xa11.txt'
, 'xa12.txt'
, 'xa2.txt'
, 'xa3.txt'
, 'xa4.txt'
, 'xa5.txt'
, 'xa6.txt'
, 'xa7.txt'
, 'xa8.txt'
, 'xa9.txt'
)
;
natsort($arr);
print_r($arr);

$arr = array
( 'a/b/c_d-e555/xa0.txt'
, 'a/b/c_d-e555/xa1.txt'
, 'a/b/c_d-e555/xa10.txt'
, 'a/b/c_d-e555/xb10.txt'
, 'a/b/c_d-e555/xb11.txt'
, 'a/b/c_d-e555/xb2.txt'
, 'a/b/c_d-e555/xb1.txt'
, 'a/b/c_d-e555/xa11.txt'
, 'a/b/c_d-e555/xa12.txt'
, 'a/b/c_d-e555/xa2.txt'
, 'a/b/c_d-e555/xa3.txt'
, 'a/b/c_d-e555/xa4.txt'
, 'a/b/c_d-e555/xa5.txt'
, 'a/b/c_d-e555/xa6.txt'
, 'a/b/c_d-e555/xa7.txt'
, 'a/b/c_d-e555/xa8.txt'
, 'a/b/c_d-e555/xa9.txt'
)
;
natsort($arr);
print_r($arr);

Open in new window

HTH, ~Ray
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38354113
To future EE visitors who might happen upon this question, there is a simple and built-in PHP function that accomplishes this objective without the necessity of added code.
http://php.net/manual/en/function.natsort.php

To learn about the PHP sorting functions you can read the online man page here:
http://php.net/manual/en/array.sorting.php
0
 
LVL 16

Author Comment

by:hankknight
ID: 38354309
natsort() does exactly what I need!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question