Jump to content

Regex to extract the integer from these source code


Raex

Recommended Posts

Hi, I need to extract the integers from the source code:

<span class=CatLevel1><a onclick="Javascript:ShowMeu('21');">Arts</a> (9768)</span>
<span class=CatLevel1><a onclick="Javascript:ShowMeu('271');">Industrial Products</a> (9321)</span>
<span class=CatLevel1><a onclick="Javascript:ShowMeu('1273');">Baby</a> (11407)</span>

What are the pattern that I can use to retrieve all the integer in the bracket (9768, 9321, 11407)? 

 

This is my php code: 

<!DOCTYPE html>
<html>
<body>


<?php
$file_string = file_get_contents('http://www.lelong.com.my/');
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);     //pattern
$title_out = $title[1];
echo $title_out;
?>
 

</body>
</html>

Thanks

Something like this?

<?php

$str = <<<EOD
<span class=CatLevel1><a onclick="Javascript:ShowMeu('21');">Arts</a> (9768)</span>
<span class=CatLevel1><a onclick="Javascript:ShowMeu('271');">Industrial Products</a> (9321)</span>
<span class=CatLevel1><a onclick="Javascript:ShowMeu('1273');">Baby</a> (11407)</span>
EOD;

if (preg_match_all('~\(([\d]+)\)~', $str,$matches)) {
 echo '<pre>'.print_r($matches[1], true).'</pre>';
} else {
  echo 'no match';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.