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!!!

7
  • for parsing xml/html you need use library for build html/xml dom tree, for parsing for example this library code.google.com/archive/p/phpquery Commented Mar 17, 2016 at 14:35
  • 2
    1) You only see the rendered/formatted text, 2) string(39) = length of <span id="sell_usd_place">1.6640</span>. Element 0 is the whole match, and Element 1 is the substring captured with the first (...) Commented Mar 17, 2016 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. Commented Mar 17, 2016 at 14:41
  • How do you parse and process HTML/XML in PHP? Commented Mar 17, 2016 at 14:55
  • @Naumov Thank you! I will have a look. Commented Mar 17, 2016 at 15:54

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.