Jump to content

Recommended Posts

I have a text field that contains the following (example text used):

 

1 2 3  4  5  6

 

As you can see the first few are split by a single space, with the last few numbers split by a double space.

 

My aim is to put each of these numbers into a table in a MYSQL database. But I'm having trouble breaking them up.

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/
Share on other sites

updated code for the latest question, first removing anything that isn't a space or a number, then replacing double-spaces with single spaces, then explode to array of numbers.

 

$str = "Hello there 1 2 3  4  5";
$str = preg_replace('/[^\s\d]/','',$str);
$str = preg_replace('/\s+/',' ',$str);
$vals = explode(" ",$str);

print_r($vals);

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150878
Share on other sites

updated code for the latest question, first removing anything that isn't a space or a number, then replacing double-spaces with single spaces, then explode to array of numbers.

 

$str = "Hello there 1 2 3  4  5";
$str = preg_replace('/[^\s\d]/','',$str);
$str = preg_replace('/\s+/',' ',$str);
$vals = explode(" ",$str);

print_r($vals);

 

This helps in getting rid of the words all together. But I was hoping to keep both the words in the array, but simply put them as one single result...

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150884
Share on other sites

i guess i would perform similar functions on the start string twice: once to isolate numbers and once to isolate letters. then make the letters (words) the first element of the array.

 

or move on to preg_match(), using regex to separate letters at the beginning and numbers at the end...

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150886
Share on other sites

Please read all the posts, not just the first one.  We have already solved the first issue, the OP has presented a second one.

 

(BTW, your regex only matches/returns spaces)

 

Good catch.

 

Perhaps a \s*\d+\s* would be a more efficient regex. *Wipes off rust* haven't been coding in a long while. I think that one searches for zero or more spaces, followed by at least one digit, followed by zero or more spaces. I changed the spacing to allow for zero spaces to take into account spaces at the beginning and end of the string.

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150895
Share on other sites

Unfortunately this is all new to me, so really struggling to get what I want from it!

 

I'm now creating a separate array for the words. But I still want it so that when there is 2 words with a space in between, the space is kept and they become one result.

 

e.g. 1 Hello there 1 2 3

 

as...

 

[0 ] Hello there

 

rather than...

 

[0 ] Hello

[1] there

 

Is this possible?

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150910
Share on other sites

<?php
  $str="Hello there 1 2 3  4  5";
  preg_match_all('/((?:[a-z]\w*\s?)+|(?:\d+))(?:\s+|$)/i',$str,$matches);
  print_r($matches);

 

I got this for the output

(
    [0] => Array
        (
            [0] => Hello there 
            [1] => 1 
            [2] => 2 
            [3] => 3  
            [4] => 4  
            [5] => 5
        )

    [1] => Array
        (
            [0] => Hello there
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
            [5] => 5
        )

)

Link to comment
https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150914
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.