1

I want to pull some car make datas from the html. This is the html data where I want to get this makes.

I tried this code but it is not working.

<?php

$data = "<select id="c1:swfield" name="c1">
<option value="--">&gt; All Makes</option>
<option value="1167">Acura</option>
<option value="1173">Alfa Romeo</option>
<option value="1564">Smart</option>
<option value="1836">Speranza</option>
<option value="1566">Ssang Yong</option>
</select>";


preg_match_all('|<select id=\"c1:swfield\" name=\"c1\">(.*?)</select>|s', $data, $matches);

print_r($matches);

?>

I'm getting some empty arrays as output. As output I want to get values between option tags such as Acura, Alfa Romeo etc.

Hamid Naghipour
  • 3,465
  • 2
  • 26
  • 55

3 Answers3

2

There are a lot of advantages to using a proper DOM parser in these sorts of cases, including knowing what data to extract.

This code uses DOMDocument and loadHTML() to load the data, then uses XPath to extract the data.

XPath can be difficult to start with, this uses //select[@id="c1:swfield"]/option which breaks down to...

  • //select - select any <select> element.
  • [@id="c1:swfield"] which has an id attribute which is c1:swfield. This is how you can specify which <select> tag you need to extract the data from.
  • /option - within that - extract the <option> elements.

This is how that code could work...

$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xp = new DOMXPath($dom);
$makes = $xp->query('//select[@id="c1:swfield"]/option');
$makeList = [];
foreach ( $makes as $make ) {
    $makeList[] = $make->textContent;
}

print_r($makeList);

and gives (with sample data)...

Array
(
    [0] => > All Makes
    [1] => Acura
    [2] => Alfa Romeo
    [3] => Smart
    [4] => Speranza
    [5] => Ssang Yong
)
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • I did consider though `option[@value!="--"]` – Nick Dec 09 '19 at 07:39
  • How i can get if i need option value="this value" also ? –  Dec 09 '19 at 16:26
  • You could use `$make->getAttribute("value")`, so `$makeList[$make->getAttribute("value")] = $make->textContent;` would give a list indexed by the value. – Nigel Ren Dec 09 '19 at 16:28
  • Then it will be like `foreach ( $makes as $make ) { $makeList[$make->getAttribute("value")] = $make->textContent; }` –  Dec 09 '19 at 16:35
  • Thanks Nick ! it worked. one more doubt if i want to only one option element for a specific value how i can put ? –  Dec 09 '19 at 16:39
  • You could change the XPath line to something like `$makes = $xp->query('//select[@id="c1:swfield"]/option[@value="1836"]');` – Nigel Ren Dec 09 '19 at 16:41
  • Ok. great ! Here how i can set the array indexes also makeList[$make->getAttribute("value")] = $make->textContent; –  Dec 09 '19 at 16:45
  • i need the array index starting 0 and then values –  Dec 09 '19 at 16:46
  • Suppose here i want to add the value and the element to my database then how i can –  Dec 09 '19 at 16:48
  • That is something completely different, if you have the data you want and not sure how to add it to the database then ask a new question (after doing some research first). – Nigel Ren Dec 09 '19 at 16:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203925/discussion-between-virendra-singh-and-nigel-ren). –  Dec 09 '19 at 16:49
0

You're not using quotes properly, try following code

    <?php

    $data = '<select id="c1:swfield" name="c1">
    <option value="--">&gt; All Makes</option>
    <option>Acura</option>
    <option>Alfa Romeo</option>
    <option>Smart</option>
    <option>Speranza</option>
    <option>Ssang Yong</option>
    </select>';


    preg_match_all('|<select id=\"c1:swfield\" name=\"c1\">(.*?)</select>|s', $data, $matches);

    print_r($matches);

    ?>
Mohit Rathod
  • 1,057
  • 1
  • 19
  • 33
0

For getting the values between option tags you can use:

preg_match_all('|(?:<select id=\"c1:swfield\" name=\"c1\">.*?)?<option value=\".*?\">(.*?)<\/option>(?:<\/select>)?|s', $data, $matches);

Here is how it works.

can
  • 444
  • 6
  • 14
  • but there is another drop down before its. –  Dec 09 '19 at 07:22
  • I've edited the answer to catch "all makes" option too. But at this point i too recommend using a DOM parser. – can Dec 09 '19 at 08:03