Handle formatting of longtext value for entity wrapper So I have a working setup where some code pulls in the latest Wikipedia extract of a page.  I then use Entities to handle a node creation, adding the contents of the extract to the longtext value of the generic body field:

$body = array();
$url = 'https://en.wikipedia.org/w/api.php?format=json&amp;action=query&amp;prop=extracts&amp;exintro=&amp;explaintext=&amp;titles='.urlencode($item['name']);
$result = @file_get_contents($url);
if ($result){
  // Oh yes!
  $decoded = json_decode($result, true);
  foreach ($decoded['query']['pages'] as $page){
    if (array_key_exists('extract', $page)){
      $body[] = t($page['extract']);
    }
  }
} else {
  // Boo, nothing found.
}

// Doesn't exist, we have to make it first. (Using Entities)
$node = entity_create('node', array('type' =&gt; 'article', 'uid' =&gt; 1));
$entity = entity_metadata_wrapper('node', $node); // $node-&gt;nid is probably better here
$entity-&gt;title-&gt;set($item['name']);
if ($body){
  $entity-&gt;body-&gt;set(array('value' =&gt; $body[0]));
}
$entity-&gt;save();
// Woo got our nid!
$nid = $entity-&gt;getIdentifier();


The problem is, I can't seem to control the output of the body at all.  It renders surrounded with paragraph tags, using html line breaks for each paragraph.  I want to surround each paragraph with paragraph tags.  I've tried using preg_match to catch newlines (\r\n) but anything I replace it with just gets rendered exactly - making paragraph tags show up in the extract text when viewing the node.

What's the best method of doing some formatting to the extract?
You just need to specify a format for your text value:

$entity-&gt;body-&gt;set(array('value' =&gt; $body[0], 'format' =&gt; 'full_html'));