For PHP5. I have a text seperated by a space. I want to split it apart and make a list. I've already converted the text to UTF-8 format. I thought I could just use this: $words = mb_split( ' +', $text ); I don't receive any errors in the log but if I echo $words the only thing I see on the page is the word array Any ideas?
$words = explode(' ', $text); foreach ( $words as $item ) { echo $item.'<br>'; } PHP: That splits the text string into an array of words.
$fulltext="I don't receive any errors in the log but if I echo $words the only thing I see on the page is the word array" $text= wordwrap($fulltext, 100, "<br />\n", true); so it will cut the text into new line when it reaches character #100.. each line will get 100 characters befor the break..besides it breaks the text only on spaces.. hope it will help u with somthin?
@martinGR I tried explode and it worked... so I will run with it until I problems creep up. Now to expand it... I now have the list of words and want to compare it to a list of stop words to remove unwanted garbage. Here's my code but I get no results. There is an error and its states: "array_diff() [<a href='function.array-diff'>function.array-diff</a>]: Argument #1 is not an array in /home/bleucube/public_html/xxx/test2/parse.php5 on line 238" $words = explode(' ', $text); foreach ( $words as $item ); $stopText = file_get_contents("http://xxx/test/stopwords.txt"); $stopWords = explode( '[ \n]+', mb_strtolower( $stopText, 'utf-8' ) ); foreach ($stopWords as $list); $text = array_diff($item, $list); echo $text;
Is that the actual code? Currently the error seems to be, that $item is not an array...it's not defined in the code you pasted here either. What are you exactly trying to do? Remove the elements of stopWords array from word list?
@MartinGR exactly. I want to take the list of words from the 1st explode and remove the words listed in stopwords.txt and display the results.
There is no special functions to do that in PHP so you have to create your own. That should do the trick. function remove_elements( $array1, $array2 ) { foreach ( $array2 as $value ) { if ( in_array($value, $array1) ) { delete_element($array1, $value); } } return $array1; } function delete_element($array, $value) { foreach ($array as $key => $val) { if ($array[$key] == $value){ unset($array[$key]); } } return $array = array_values($array); } PHP: The first function takes 2 arguments, both arrays. First one is the words array and second one is stopwords. It loops through all the stopwords and checks, if there is a matching value in words array. If there is it deletes it. Function return word array (1. argument) with stopwords removed. Just use it like $words = remove_elements($words, $stopWords); PHP: