/    Sign up×
Community /Pin to ProfileBookmark

XPath Expression To Deal With BR Element

I have a bunch of HTML documents with a P element and ‘id’ attribute set to ‘title’. Like so:

[code=html]<p id=”title”>
Title of the document
[/code]

In some cases, I have a title that has a forced line break:

[code=html]<p id=”title”>
This Is A Title Of A Document<br>With A BR Element In It
[/code]

I have created an UpdateAndSynchronize.php document that scans a tree where all my web documents are, loads the document (using DOMDocument::loadHTML()), sets up the XPath object, and extracts the info I want to put in the MySQL database.

My XPath expression to get the document title is:

[code=php]$docTitle = $htmlXPath->query(‘.//p[@id=”title”]’)->item(0)->textContent;
$docTitle = trim(str_replace(array(“n”, “rn”, “r”, “t”), ” “, $docTitle));
[/code]

$htmlXPath is an XPath object.

I had to add the second line to get rid of leading and trailing whitespace.
My problem is the str_replace() is not working, because the <br> element in the XPath query is probably being converted (translated?) to some other character.

The question is:

[U][B]How should I be setting up my XPath->query() to convert <br> elements into a single space character?
[/B]
[/U]

Also, is there a good reference (book? web pages?) that show how to set up XPath queries (evaluations?) with lots of examples?

to post a comment
PHP

1 Comments(s)

×

Success!

Help @seniseven spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...