【问题标题】:Get text before or after html tags using Xpath使用 Xpath 在 html 标记之前或之后获取文本
【发布时间】:2015-01-28 11:53:14
【问题描述】:

我有html,我在这里简化,我需要写一个xPath来获取电话号码。

<td>
    <font>
        <b>
            <font size="2">
                <a href="#">Some link</a>
            </font>
        </b>
        <br>
        Abc Address
        <br>
        Country name
        <br>
        (123) 456-7890
        <hr>
        A sentence here..
        <img src="/images/abc.gif">
    </font>
</td>

我可以将锚标记内的文本提取为,

->filterXPath('//font//b//a')->extract('_text'); //returns some link

如何在最后一个&lt;br&gt; 标记之后或第一个&lt;hr&gt; 标记之前提取此文本(123) 456-7890?我访问过this link,但我无法正确理解。

我也试过这个:

->filterXPath('//font//br[last()]')->extract('_text'); // returns nothing but empty

【问题讨论】:

    标签: php html xpath


    【解决方案1】:

    选择最后一个br,然后选择它的第一个文本兄弟:

    //font/br[last()]/following-sibling::text()[1]
    

    【讨论】:

    • 天哪,我必须以一种特别的方式感谢你@choraba,你是救生员。它工作得很好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2011-10-02
    • 2018-05-27
    • 1970-01-01
    • 2017-12-13
    相关资源
    最近更新 更多