1

I am PHP newbie and I hope somebody can help.

There is a site which contains following HTML:

<span id="sell_usd_place">1.6640</span>

My code is below:

<?php
$data = file_get_contents('http://www.a_bank.com/currency-rates/');
$regex = "#<span id=\"sell_usd_place\">(.*?)</span>#";
preg_match($regex,$data,$matchKapitalSell);
var_dump($matchKapitalSell);
echo $matchKapitalSell[0]."<br>";
echo $matchKapitalSell[1];
?>

What I expect is that in the output I will get: "<span id="sell_usd_place">1.6640</span>" as it is what I set as a pattern.

Bit what I get is (I am using XAMPP to check the code):

array(2) { [0]=> string(39) "1.6640" [1]=> string(6) "1.6640" } 1.6640
1.6640array(0) { }

Could anybody explain please:

  1. Why I get "1.6640" only and not <span id="sell_usd_place">1.6640</span>

  2. What is "39" in "string(39)" in above output?

Thank you in advance!!!

Rat2good
  • 119
  • 9
  • for parsing xml/html you need use library for build html/xml dom tree, for parsing for example this library https://code.google.com/archive/p/phpquery/ – Naumov Mar 17 '16 at 14:35
  • 2
    1) You only see the rendered/formatted text, 2) `string(39)` = length of `1.6640`. Element 0 is the whole match, and Element 1 is the substring captured with the first `(...)` – Wiktor Stribiżew Mar 17 '16 at 14:36
  • 4
    The string(39) means that the string is of length 39. I have a gut feeling that you var_dumped it and looked at in a web browser. Look at the source code. The web browser will hide the HTML tags from you. – kainaw Mar 17 '16 at 14:41
  • [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/q/3577641/476) – deceze Mar 17 '16 at 14:55
  • @Naumov Thank you! I will have a look. – Rat2good Mar 17 '16 at 15:54
  • @Wiktor Stribiżew Great Answer! Thank you! – Rat2good Mar 17 '16 at 15:56
  • @kainaw Thank you! That is great answer!!! I see now! – Rat2good Mar 17 '16 at 16:11

1 Answers1

3

Use DOM instead of a regex. Regular expressions aren't fitted to reliably parse HTML.

Example:

$html = file_get_contents('http://www.a_bank.com/currency-rates/');
$doc = new DOMDocument();
$doc->loadHTML($html);
$span = $doc->getElementById('sell_usd_place');
$value = $span->nodeValue;

echo $value; # 1.6640
hek2mgl
  • 152,036
  • 28
  • 249
  • 266