Reputation: 4848
I am trying to replace a <br>
with a space within a string in PHP, however it does not seem to work. How would I go about correcting what I did to make this work?
The line of code that I have tried is the following:
$keyword = str_replace("<br>", " ", getTextBetweenTags($html, "h4", "ctlAuthor"));
The string that is being returned in code looks like the following:
Dörte Kreher<br>Humboldt-Universität<br>Institut für Mathematik
The string that is being return to the PHP page is the following:
Dörte KreherHumboldt-UniversitätInstitut für Mathematik
The desired string that should be returned to the PHP page:
Dörte Kreher Humboldt-Universität Institut für Mathematik
Here is the getTextBetweenTags() function:
function getTextBetweenTags($string, $tagname, $tagid)
{
$dochtml = new DOMDocument();
$dochtml->loadHTML($string);
$prgs = $dochtml->getElementsByTagName($tagname);
$pcls = array();
foreach($prgs as $prg)
{
if ($prg->getAttribute('id') == $tagid){
$pcls[] = $prg->nodeValue;
}
}
return $pcls[0];
}
Upvotes: 2
Views: 2777
Reputation: 4848
Because the strings are coming from the NodeValues of elements within a DOMDocument, the HTML needs to be preserved. In order to resolve this issue, the line of code:
$pcls[] = $prg->NodeValue;
Was replaced with:
$pcls[] = $prg->ownerDocument->saveHTML($prg);
And then the HTML tags were able to be replaced with the desired output as follows:
$keyword = str_replace("<br>", " ", getTextBetweenTags($html, "h4", "ctlAuthor"));
Upvotes: 0
Reputation: 98921
Why don't you use strip_tags? i.e:
$keyword = strip_tags(getTextBetweenTags($html, "h4", "ctlAuthor"));
strip_tags
Strip HTML and PHP tags from a string
string strip_tags ( string $str [, string $allowable_tags ] )
This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given
str
. It uses the same tag stripping state machine as the fgetss() function.
Update based on your comments:
$string = "Dörte Kreher<br>Humboldt-Universität<br>Institut für Mathematik";
$notags = preg_replace('/<[^>]*>/', ' ', $string );
echo $notags;
Output:
Dörte Kreher Humboldt-Universität Institut für Mathematik
Upvotes: 0
Reputation: 188
try this one, it´s replace all html code in the string
$keyword = preg_replace("#<[^>]+>#", ' ', getTextBetweenTags($html, "h4", "ctlAuthor"));
Upvotes: 0
Reputation: 45490
Try with a non breaking space html
$keyword = str_replace("<br>", " ", getTextBetweenTags($html, "h4", "ctlAuthor"));
Upvotes: 3