0

I have a pattern that selects phone numbers in bock of html and its works perfect (I use it to make numbers show only when click on "Show"number" with a div a wrap the number in with "preg_replace".

$pattern = '!(\b\+?[0-9()\[\]./ -]{7,17}\b|\b\+?[0-9()\[\]./ -]{7,17}\s+(extension|x|#|-|code|ext)\s+[0-9]{1,6})!i';

The problem is its selects numbers inside IMG A tags.

For example: domain.com/images/09/2015 , the number "09/2015" will be selected.

Or a link domain.com/2015/09 "2015/09" will be selected by the pattern.

How could i set the pattern is not inside those tags?

This is a not question how to extract data from html. I know there are a few other ways. The script works and matches the numbers i need. my question is how could i extend the pattern in this script to not include matches between A IMG tags.

BenB
  • 2,747
  • 3
  • 29
  • 54
  • 1
    Thay would be extremely hard or even impossible to do correctly with regular expression. You need to use HTML parsing utilities like DOMDocument and then use that regex on DOMText instances. – Tomasz Kowalczyk Dec 28 '15 at 20:32
  • 1
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Dec 28 '15 at 20:35
  • @chris85 This is a different quistion. I have the script and its working. My qustion is how could i extend the pattern to not include matches between A IMG tags – BenB Dec 28 '15 at 20:41
  • Use `(*SKIP)(*F)` technique. – Wiktor Stribiżew Dec 28 '15 at 20:49
  • It is a different question, but it is an approach you could use to solve it. – chris85 Dec 28 '15 at 21:05

1 Answers1

1

I solved this with the help of php_simple_dom like this:

require ("simple_html_dom.php");
$html = str_get_html($content);
$content = '';
foreach ( $html->find('p') as $line){
  if(strpos($line,'<a') == false && strpos($line,'<img') == false &&   strpos($line,'< img') == false && strpos($line,'< a') == false) {
    $content .= preg_replace($pattern, $part_1_before_phone_href .'$1' .$part_2_after_phone_href . $show_number_text . $part_3_close_tag ,$line);
  }
  else {
    $content .= $a;
  }
}
BenB
  • 2,747
  • 3
  • 29
  • 54