tags:

views:

68

answers:

2

Guys,

Not gonna lie, I'm terrible at regex.

How would I be able to do this guys:

$string = '>Data 1-23</a>';
$string = '>Datkl3</a>';
$string = '>RA Ndom</a>';

And pull out the "Data 1-23" from inside the above string using regex? And if I have multiple ones of this, how would I be able to put all of the matched strings into an array?

+5  A: 

If you're looking for the text in hyperlinks your best bet is SimpleHTMLDom. Here's a quick example:

$html = file_get_html('http://www.amazon.com/');
foreach($html->find('a') as $element)
  echo $element->innertext . '<hr/>';

Parsing the DOM tree gives much more reliable results than a simple regexp

Hope that helps!

Al
+3  A: 
<?php
  $string = ">Data 1-23</a>";
  $pattern = '/>([^<]*)</a>/';
  preg_match($pattern, $subject, $matches);
  print_r($matches);
?>

Should give you what you want, as far as I understand.

Håkon