2

I am using XPath to get some paragraphs from my DomDocument. This works fine and returns me the desired data.

The problem is that when doing:

foreach ($paragraph->childNodes as $child) {
    $node .= $paragraph->ownerDocument->saveHTML($child);
}

If there is an initial line break, this will be keept, and I would like to get rid of all linebreaks.

I tryed:

$node = trim($node); // Does not work

Then:

$breaks = array("\r\n", "\n", "\r");
$node = str_replace($breaks, " ", $node); // Doesn't work

I also tryed:

$paragraph->ownerDocument->formatOutput = false;
$paragraph->ownerDocument->preserveWhiteSpace = false;

Did not work.

Any Idea on how to get rid of those line breaks ?

Thank you in advance.

EDIT

Here is an example of the $node input:

<b>Keywords: </b>marine fungus; sediment; anthranilic acid; <i>Penicillium
 paneum</i>; cytotoxicity

Apparently, the problematic character is 
 what is this special character ?

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

3 Answers3

3
$node = preg_replace(array('/\r/', '/\n/'), '', $node);

This will do the trick.

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
1

try preg_replace

   $string =  preg_replace("#<br\s*/?>#i", "", $string);

or Try

preg_replace( "/\r|\n/", "", $yourString);
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40
1

Apparently, it is the character &#13; that is causing te linebreak. I never heared about this special character, no idea how it is produced.

So basicaly, what I had to do is to replace it.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265