What potential pitfalls should be considered when using the explode() and wordwrap() functions in PHP to manipulate strings?
When using the explode() function in PHP to split a string into an array based on a delimiter, be cautious of potential issues such as empty array elements if the delimiter appears multiple times consecutively. To avoid this, you can use the array_filter() function to remove any empty elements from the resulting array. Similarly, when using the wordwrap() function to wrap a string to a certain number of characters per line, be aware that it may split words in the middle. To prevent this, you can set the third parameter of wordwrap() to true to ensure that words are not broken.
// Using array_filter() with explode() to remove empty elements
$string = "apple,,banana,orange,,";
$delimiter = ",";
$array = explode($delimiter, $string);
$array = array_filter($array);
// Using wordwrap() with third parameter set to true to prevent breaking words
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$wrapped_string = wordwrap($string, 20, "\n", true);