0

i try to return a number on a div , i want to have the "01 55 33 44"

     <div data-phone="01 55 33 44" class="agency_phone ">
     Phone
     </div>

I have try :

   $url = "myurl"; 
    $raw = file_get_contents($url); 
    preg_match('/<div data-phone="(.*)"class="agency_phone "/isU',$raw,$output); 
    echo $output[1];  

I have no return, somone have an idea ?

Thanks in advance.

Jeed
  • 25
  • 1
  • 7

4 Answers4

1

index.php file have following content.

<?php
   $url = "test.php"; 
   echo $raw = file_get_contents($url); 
   preg_match('/data-phone="(.*)" class/', $raw, $output);
   echo $output[1];
?>

And other file source.php which have html tags.

<div data-phone="01 55 33 44" class="agency_phone ">
  Phone
</div>

It will return followig array

Array
(
  [0] => data-phone="01 55 33 44" class
  [1] => 01 55 33 44
)
Divyesh Patoriya
  • 518
  • 3
  • 15
  • It's worth noting that regex-based solutions often require maintenance to adapt to tiny formatting changes in source HTML (just imagine they swap `data-phone` and `class` or insert another attribute in between). And of course they will always fail in edge cases. – Álvaro González Jun 14 '16 at 12:18
1

To begin with, your regexp expects exactly zero spaces after the attribute, thus it won't match your actual HTML with exactly one space:

/<div data-phone="(.*)"class="agency_phone "
<div data-phone="01 55 33 44" class="agency_phone ">

In any case it's very hard to write a decent HTML parser from scratch using regular expressions. The easiest way is DOM and XPATH, e.g.:

<?php

$html = '
    <div data-phone="01 55 33 44" class="agency_phone ">
     Phone
     </div>
     <p>Unrelated</p>
     <div>Still unrealted</div>
        <div data-phone="+34 947 854 712" class="agency_phone ">
          Phone
          </div>

';

$dom= new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$phones = $xpath->query('//div/@data-phone');
foreach ($phones as $phone) {
    var_dump($phone->value);
}
string(11) "01 55 33 44"
string(15) "+34 947 854 712"
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • @ÁlvaroGonzález what would be the expression be if I wish to get the data-phone of a specific class? – SML Jun 14 '16 at 11:34
  • //div[@class="classname"]/@data-phone – splash58 Jun 14 '16 at 11:40
  • Hum, filtering by class name in XPATH is not straightforward. After all it's a language that was developed with XML in mind, not HTML. You can check [Selecting a css class with xpath](http://stackoverflow.com/questions/8808921/selecting-a-css-class-with-xpath) for some suggestions (accepted answer is just awesome). – Álvaro González Jun 14 '16 at 12:12
  • @ÁlvaroGonzález thanks for the info, much appreciated – SML Jun 14 '16 at 12:57
0

Is it the missing space?

[edit] put full files here for reproduction purposes [/edit]

This works:

// file url.html
<div data-phone="01 55 33 44" class="agency_phone ">
     Phone
     </div>

and:

<?php
// file test.php
$raw = file_get_contents('url.html');
preg_match('/data-phone="(.*)" class/',$raw,$output);
echo $output[1]; // 01 55 33 44
0

Tested with a html file on local host, seems to work fine.

<?php
$url = "myurl"; 
$subject = file_get_contents($url); 
$pattern='<div data-phone="(.*)" class="agency_phone ">';
preg_match($pattern, $subject, $output);
echo $output[1];    
?>
SML
  • 1,235
  • 6
  • 17