Harish Ninge Gowda
Harish Ninge Gowda

Reputation: 441

how to remove multiple whitespaces and newline character from HTML entity

I am trying to implement a crawler using codeigniter and simplehtmldom.

$page = "URL to be Crawled";
$html = file_get_html($page);
$ad_description = $html->find('#ad_description',-1);
$description = $ad_description->innertext;

$description contains multiple consecutive spaces and newline which I need to convert in to single appearances.

I tried

str_replace("\n\r",' ',$description),
reduce_multiples($ad_description->innertext,"\r")
preg_replace("/[\r\n]+/", "\n", $description)
ascii_to_entities($description,ENT_HTML5, "ISO-8859-1")

and many other possible options but without success. Any help would be appreciated.

Upvotes: 1

Views: 432

Answers (1)

splash58
splash58

Reputation: 26153

i think that pref_replace does work

$description = "This
is      a
test   string
";

echo $description = preg_replace('/\s+/', ' ', $description); // This is a test string 

Upvotes: 0

Related Questions