1

With PHP if you have a string which may or may not have spaces after the dot, such as:

"1. one 2.too 3. free 4. for 5.five "

What function can you use to create an array as follows:

array(1 => "one", 2 => "too", 3 => "free", 4 => "for", 5 => "five") 

with the key being the list item number (e.g the array above has no 0)

I presume a regular expression is needed and perhaps use of preg_split or similar? I'm terrible at regular expressions so any help would be greatly appreciated.

5
  • possible duplicate of Regex match for text Commented Sep 29, 2012 at 17:45
  • [0-9]\.(\s)?[a-z] perhaps that helps? Commented Sep 29, 2012 at 17:47
  • 1
    regexp would be sweet, but still you know there is always a dot. You don't need regexp. Split on that (explode()) an use the last character in each string to determine the key for the value found in next string. IN EDIT: lol: but then again 11684 is correct. So much more sweet ... Commented Sep 29, 2012 at 17:47
  • @opaque why don't you post that as an answer? Commented Sep 29, 2012 at 17:49
  • @opaque Really? Is my regex correct? And why 'again'? Commented Sep 29, 2012 at 17:50

2 Answers 2

2

What about:

$str = "1. one 2.too 3. free 4. for 5.five ";
$arr = preg_split('/\d+\./', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);
Sign up to request clarification or add additional context in comments.

2 Comments

That works nicely - many thanks, one thing though... it doesn't keep the number as the key - any ideas?
In that case, you may insert an artificial element at the beginning of the string and remove the first element after using preg_split(), like $arr = preg_split('/\d+\./', '0.n' . $str, -1, PREG_SPLIT_NO_EMPTY); unset($arr[0]);
-1

I got a quick hack and it seems to be working fine for me

        $string = "1. one 2.too 3. free 4. for 5.five ";

    $text_only = preg_replace("/[^A-Z,a-z]/",".",$string);
    $num_only = preg_replace("/[^0-9]/",".",$string);

    $explode_nums = explode('.',$num_only);
    $explode_text = explode('.',$text_only);

    foreach($explode_text as $key => $value)
    {
        if($value !== '' && $value !== ' ')
        {
            $text_array[] = $value;
        }
    }

    foreach($explode_nums as $key => $value)
    {
        if($value !== '' && $value !== ' ')
        {
            $num_array[] = $value;
        }
    }

    foreach($num_array as $key => $value)
    {
        $new_array[$value] = $text_array[$key];
    }

    print_r($new_array);

Test it out and let me know if works fine

3 Comments

Wow now even creator marked answers are getting minus 1. Will surely like to know the reason
If you insist. You wrote 25 lines of PHP code for what a single line of regex would accomplish. OP being happy with any subpar code snippet handed to him doesn't make this upvoteworthy. Sorry.
Surely agree with you, at that point of time none of the answers worked for him, so choose my own way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.