I am extracting some strings from HTML pages and sometimes i end up with contents like this:
...
<div>
This is a sample text that extends
over several lines, with linebreaks
in the HTML code and with indentation
spaces to the left.
</div>
...
When I extract the text within the <div>
and store it in a string it will contain both a lot of extra spaces and linebreak characters. I wish to convert the string to a "one-line-string", i.e. removing unnecessary spaces and linebreaks. Here's a conceptual part of the code:
<?php
$my_textpart = extract_textpart_from_div();
$my_string = remove_unwanted_whitespaces($my_textpart);
// --> $my_string = "This is a sample text that extends over several lines, with linebreaks in ... etc ..."
?>
Is there a neat or smart PHP function that may help me here, or will I be forced to write a function where I use str_replace
in a loop to remove double spaces and linebreaks until all extra whitespaces are removed?