How do I pick a line from a field that has no bounds and break into an array?

Asked

Viewed 46 times

-1

I have the code that takes a string from a field in the example below:

material plant part flow light

This string has no delimitations and on top of that, as you can see, the words of the string I need to break have different characters running my code below the string appears like this:

material plantape çavazolu z

the string above is just an example of my code problem, someone knows how to solve it?

$text = $_POST["text"];
$plant = $_POST["combox"];

    mysql_query("Delete from flex_report.tab_mtm_ativos where planta = '$plant'");

    $count = strlen($text);

    for($i=0;$i < $count;$i++){
        if(strstr($text,"-")){
            $replace = str_replace("-","",$text);   
            $novaString = chunk_split($replace,12,".");
        }else{
            $novaString = chunk_split($text,9,".");
        }
    }

    //quebra array em string
    $bat = explode('.',$novaString);
    print_r($bat)."<br>";

I need the string to split:

string = material planta peça luz chocolate

i want to break this string up like this in the example below:

string divide

0 => material
1 => plant
2 => piece
3 => light
4 => chocolate

That’s what I need most with the code I made

0 => material
1 => plantape
2 => çaluzcho
3 => colate

  • If you don’t explain what you want, it’s hard for us to help. What is the result you want to get, how does the output have to stay? Do you want to take word for word, letter for letter or what? And how will you use the output?

  • as I could not make the explode with space, I put a "." to delimit the string and played inside this variable

  • The problem is that your count is delimiting the number of characters in the string. 8 characters in each item. Do like this: $words = explode(" ", $text); $Count = Count($words);

  • 1

    See if that’s it, $arr = explode(' ','material planta peça vazo luz') print_r($arr);

  • 1

    can solve, I used json_encode function to delimit n r and then I blew up breaking with n r

  • I’m curious to know how you made json_encode() add line breaks

  • Actually you didn’t solve it, you did another stunt in place of the original. The correct solution is the one @rray mentioned, and the question after editing is a duplicate of http://answall.com/questions/22063/ (only changes the delimiter)

Show 2 more comments

1 answer

1

In the string you entered there is a delimiter, the space.

Just "blow up" the spaces.

$arr = explode(' ', $string);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.