0

i tried to get specific string from html file, i already get the file name. but i want get piece of string from the different file name. here's the example i was tried.

html

<TABLE class="tab1" border="1" cellpadding="0" cellspacing="0" summary="">
<TR>
<TH align=left colspan=2 bgcolor=#0066CC><H1> &nbsp;Start RIP Job</H1>
</TH>
</TR>
<TR>
<TH align=left> &nbsp; &nbsp;File:
</TH>
<TD class="td1" align=left> &nbsp; &nbsp;WALUYO GROUP (LUTFI) - 240 - 
 ITHINK\T7\T7-FROZ-45X163 - 20 PCS.tif&nbsp; &nbsp;
 </TD>
</TR>
</TABLE>

<TABLE class="tab1" border="1" cellpadding="0" cellspacing="0" summary="">
<TR>
<TH align=left colspan=2 bgcolor=#0066CC><H1> &nbsp;Start RIP Job</H1>
</TH>
</TR>
<TR>
<TH align=left> &nbsp; &nbsp;File:
</TH>
<TD class="td1" align=left> &nbsp; &nbsp; Gigas G - 100x100 - 1x.tif&nbsp; &nbsp;
</TD>
</TR>
</TABLE>

CODE I TRIED,

foreach (glob("C://xampp/htdocs/Log/Flora/*.HTML")as $html1)
{ 
if (file_exists($html1)) 
{ 
$source1 = file_get_contents($html1);
$dom1 = new DOMDocument();
$dom1->loadHTML($source1);
$xpath = new DOMXPath($dom1);
$textList1 = $xpath->query("//table");
    foreach ( $textList1 as $text1 )
{
    $files   = $xpath->evaluate(

"string(descendant::tr[th[contains(text(),'File')]]/td/text())",
                   $text1);
        $threee  = $files;
    $query = "INSERT INTO CMP.LOG_FILE(FIL_NM) VALUES 
('".utf8_decode($threee)."')";
    $result = mysql_query($query); 
}}}

RESULT :

1. WALUYO GROUP (LUTFI) - 240 - ITHINK\T7\T7-FROZ-45X163 - 20 PCS.tif
2. Gigas G -100x100- 1x.tif

Now i want is just get the size, ex:

1. 45X163
2. 100x100

2 Answers 2

1

you can get the part of size string like this.

$pattern = "/- ?(\d+[Xx]\d+) ?-/";
preg_match($pattern, $files, $matches) ;
print($matches[1]) ;
Sign up to request clarification or add additional context in comments.

Comments

1

Well, if all of your filenames so kindly contain the size in just that way, and no additional size-like strings, I would just go with:

$size = preg_match('/(\d+x\d+)/', $filename, $match) ? $match[1] : "Not found";

If you have a formal definition for the size component within the filename, you could refine the pattern. For example, if spaces are allowed (240 x 240), you could change it to:

$size = preg_match('/(\d+)\s*x\s*(\d+)/', $filename, $match) ? ($match[1].'x'.$match[2]) : "Not found";

And so forth. However, having said that, if you have access to the files, you could also use getimagesize to actually get the size of the image rather than depend on the filename. So:

$filesize = getimagesize($filepath);
$size = $filesize[0].'x'.$filesize[1];

3 Comments

Yes. Using the second preg_match example, you can multiply $match[1] * $match[2] to get that. I added the getimagesize use case. You can also multiply $filesize[0] * $filesize[1] in that example.
<TD class="td1" align=left> &nbsp; &nbsp;47.0 x 173.0cm&nbsp; &nbsp; </TD>........ how about this, i dont understand preg match
I think an explanation of regular expressions and how they are implemented in PHP is probably beyond the scope (and length!) of a comment, but you can read about it here - its probably worth your time if you are planning to dissect filenames and the like. I actually prefer the getimagesize in this case - filenames which are supposed to tell you about the files are usually notoriously unreliable, and its easy enough to get the image size from the file content.

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.