Extract HTML value

Asked

Viewed 160 times

2

I get the html and a part of it from where I need to extract the data and this one

         <td class="address-list-item">
    NOme silva    <div class="modal-content" style="display:none;">
        <div class="address-list-modal">
            <h2 class="title title-5">Excluir endereço</h2>
            <h3 class="title title-7">Deseja realmente excluir esse endereço?</h3>

            <div class="address-default-title">NOme</div>
            <div class="address-default-item">Rua  </div>
            <div class="address-default-item">CEP - SETOR </div>
            <div class="address-default-item">Cidade - Estado</div>

            <a onclick="deleteAddress(00000)" class="btn btn-9" "="" href="javascript::void(0)">Sim</a>
            <a onclick="clModal()" class="btn btn-9" href="javascript::void(0)">Não</a>
        </div>
    </div>
</td>
<td class="address-list-item">

    Rua  -
    cep - setor -
    Cidade - Estado
</td>
<td class="address-list-item">
    (00) 0000-0000    </td>
                        <td class="address-list-item is-last">
                            <a class="sel-link-edit-addicional-0000 address-list-item-link" href="/customer/address/edit/?address_id=000">
                                Editar                            </a>
                            <span class="bt_separator">|</span>
                            <a id="bt-delete-0000" class="address-list-item-link" href="javascript::void(0)" onclick="confirmationModal(this,0000)">
                                Excluir                            </a>
                        </td>
                    </tr>

and need to extract only the City - State

so I did my regex like this

    if (preg_match("#- (.*?) - \b[A-Z][A-Z]#", $this->http_response, $match))
    $estado = $match[0];

return $estado;

only that is not returning to the City - State , and already all subject related to the subject in stackoverfolow and I found nothing that can help me in this problem :/

  • The ER you tried to ride doesn’t make sense.. but anyway, it’s complicated because it doesn’t have a logical reference to identify the stretch you want to extract. What I suggest is, if the data always returns in the same line, you could just go straight to the corresponding line... I think that would be much easier because taking the line, just give a explode() to separate State and City. This is a mere suggestion.. there are other ways to solve, including with ER, but I prefer to recommend the simplest way

  • Or you can use a parser, which also makes life much easier, for example: PHP Simple HTML DOM Parser or http://htmlparsing.com/php.html

1 answer

1

If the field at all times comes formatted like this:

Rua  -
cep - setor -
Cidade - Estado

You can use PHP’s explode function

$endereco = explode ( '-' , endereco );
$cidade = $endereco[2];
$estado = $endereco[3];

or javascript:

var endereco = document.querySelector(".address-list-item").innerHTML.split("-");
var rua = endereco[0];
var cep = endereco[1];
var cidade = endereco[2];
var estado = endereco[3];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.