php fetch <p> content

Asked

Viewed 32 times

0

How can I catch the 'Valoor'

<p id="sinopse2" style="display:block !important;">VALOOR</p>

I tried that code:

preg_match_all("#<p id=\"sinopse2\">(.*?)<\/p>#s", $string, $encontrou);

But I was unsuccessful ;(

2 answers

1


Use the following code:

preg_match_all("/<p[^>]*id=\"sinopse2\"[^>*]*>(.*)<\/p>/", $html, $output);
var_dump($output);

Explanation of the Regex:

<p.+id=\"sinopse2\"[^>*]*>(.*)<\/p>
│  │       │            │     │   │
│  │       │            │     │   └────── Informa o limite da seleção
│  │       │            │     └────────── Captura o grupo que está entre `<p>`
│  │       │            └──────────────── Seleciona todos os caracteres até `>`
│  │       └───────────────────────────── Informa o ID do elemento
│  └───────────────────────────────────── Selecione tudo até o ID
└──────────────────────────────────────── Informa a tag

Demonstration

  • yes, the code worked very well now !

0

$qtd = '<p id="sinopse2" style="display:block !important;">VALOOR</p>';

$out = trim(strip_tags($qtd));

echo $out; //VALOOR

strip_tags - Removes HTML and PHP tags from a string

Example - ideone

Another example:

$qtd = '<p id="sinopse2" style="display:block !important;">Aqui temos o conteudo da tag p que não é necessariamente VALOOR</p>';

$out = trim(strip_tags($qtd));

echo $out; //Aqui temos o conteudo da tag p que não é necessariamente VALOOR

running - ideone

  • this way would only work if the content of the line was always "VALOOR" rs, I think I passed my idea a little confused, because I used as example 'VALUE' not to be empty the tag,the content can be several, more obliged to intention

  • @Paulopreviatto, I didn’t understand your comment conteudo da linha sempre fosse "VALOOR" You saw it running on the ideone?

  • so now I understand the code, it takes everything around "VALOOR" and leaves only the content, that’s it ??

  • @Paulopreviatto, it removes the tags and returns the content (text)

  • now it became clear to me, thank you also for showing one more way to get a value of <p>

  • @Paulopreviatto, it’s not just <p> could be from any html tag

Show 1 more comment

Browser other questions tagged

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