php - RegEx for matching a HTML string

760

I'm trying to do apreg_match_all, which does not work.

preg_match_all(
  '!<div itemprop="description"><p><span ><span ><span >(.*?)</span></span></span></p></div>!',
  $html,
  $matches1,
  PREG_SET_ORDER
);
foreach ($matches1 as $soge2){print_r($soge2);}

How do I make it work?

900

Answer

Solution:

It may not be the best idea to parse HTMLs with RegEx, which seems that you may want to do. However, based on your question, I'm guessing that you may wish to get all data from this tag using (.*):

<span class="page_speed_1772971076"></span>

It is possible to do so. First, you may design yourpreg_match_all(); based on:

preg_match_all($pattern, $subject, $matches);

which in this case, your subject is your HTML input, and I'm not sure, but your pattern might be:

/<span style=\"font-size:14px;\">([\w\s]+)(<\/span>)/s 

Code

$html = '<div itemprop="description"><p><span ><span ><span >Alphanumeric Words If You Wish To Match</span></span></span></p></div>';

preg_match_all('/<span style=\"font-size:14px;\">([\w\s]+)(<\/span>)/s', $html, $matches1, PREG_SET_ORDER);
var_dump($matches1);

foreach ($matches1 as $soge2) {
  print_r($soge2);
}

Output

Array
(
    [0] => <span class="page_speed_1772971076">Alphanumeric Words If You Wish To Match</span>
    [1] => Alphanumeric Words If You Wish To Match
    [2] => </span>
)

Pattern

You may use a tool to design your desired pattern, such as:

enter image description here

People are also looking for solutions to the problem: php - How can I comment an instagram post with Graph API

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.