How to remove unwanted enters or spaces from curled HTML data?

This article was written by in July 28, 2015, & may not be posted on other sites!
Original source url for this article: How to remove unwanted enters or spaces from curled HTML data?

How to remove unwanted enters or space from curled HTML data?
Using the function curl to get HTML data may also include unwanted enters or spaces from the original source. This can cause problems when printing the code, for example auto generated strange elements like random closing paragraphs </p> or unwanted line breaks <br />. To clean up the incoming code and remove unwanted enters or spaces, follow these steps.

How to remove unwanted enters or spaces from curled HTML data?

 

Remove unwanted enters or spaces from curled HTML data

To clean up the incoming HTML code and remove unwanted enters or spaces use this regEx example. The function meaning replace all the unwanted enters or spaces with nothing in variable incoming data:
$result = preg_replace('/^\s+|\n|\r|\s+$/m', '', $incomingData);

Another useful example:
// Before any output
ob_start();
// End of file
$output = ob_get_clean();
echo preg_replace('/^\s+|\n|\r|\s+$/m', '', $output);

 

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *