Jump to content

Splitting text


adamyork

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.