0

I'm writing some codes on php. Now I want to grap words which include only alphabetic and numeric character. But I can't. I am writing my codes here. In actualy I found this regex in this site (Allow only [a-z][A-Z][0-9] in string using PHP).

$fp = fopen('C:\wamp\www\curl\5510.doc','w');
fwrite($fp, $data); 
fclose($fp);
$file = doc2text('C:\wamp\www\curl\5510.doc');
@preg_match_all("/^[a-zA-Z0-9]+$/", file_get_contents($file), $fileOnlyAlphabetic);
print_r($fileOnlyAlphabetic);

And result is here

Array ( [0] => Array ( ) [1] => Array ( ) )

Please help me ...

Community
  • 1
  • 1

3 Answers3

1

First point, you should avoid writing the '@' symbol in front of the preg_match_all call, because then you're hidding potential errors.

Secondly, is probable that a .doc file doesn't have any line with only alphanumeric characters, without spaces, without punctuation symbols... or without non-printable symbols. So, the code is running OK, but you aren't using a good pattern.

You should remove the $ character from the regexp, and also the ^ character. The ^ character is to indicate a line start, and the $ character is to indicate a line end.

Is also probable that doc2text returns you the file content, not its name or file descriptor, so you should also remove the file_get_contents call made inside the preg_match_all.

Try with something like

$fp = fopen('C:\wamp\www\curl\5510.doc','w');
fwrite($fp, $data); 
fclose($fp);
$file = doc2text('C:\wamp\www\curl\5510.doc');
preg_match_all("/[a-zA-Z0-9]+/", $file, $fileOnlyAlphabetic);
print_r($fileOnlyAlphabetic);

Hope it helps.

castarco
  • 1,368
  • 2
  • 17
  • 33
0

@stribizhev Here is the code you want

function doc2text($userDoc) {
 $fileHandle = fopen($userDoc, 'r');
 $word_text = @fread($fileHandle, filesize($userDoc));
 $line = "";
 $tam = filesize($userDoc);
 $nulos = 0;
 $caracteres = 0;
 for($i=1536; $i<$tam; $i++)
 {
    $line .= $word_text[$i];

    if( $word_text[$i] == 0)
    {
        $nulos++;
    }
    else
    {
        $nulos=0;
        $caracteres++;
    }

    if( $nulos>1996)
    {   
        break;  
    }
}

//echo $caracteres;

$lines = explode(chr(0x0D),$line);
//$outtext = "<pre>";

$outtext = "";
foreach($lines as $thisline)
{

    $tam = strlen($thisline);
    if( !$tam )
    {
        continue;
    }

    $new_line = ""; 
    for($i=0; $i<$tam; $i++)
    {
        $onechar = $thisline[$i];
        if( $onechar > chr(240) )
        {
            continue;
        }

        if( $onechar >= chr(0x20) )
        {
            $caracteres++;
            $new_line .= $onechar;
        }

        if( $onechar == chr(0x14) )
        {
            $new_line .= "</a>";
        }

        if( $onechar == chr(0x07) )
        {
            $new_line .= "\t";
            if( isset($thisline[$i+1]) )
            {
                if( $thisline[$i+1] == chr(0x07) )
                {
                    $new_line .= "\n";
                }
            }
        }
    }
    //troca por hiperlink
    $new_line = str_replace("HYPERLINK" ,"<a href=",$new_line); 
    $new_line = str_replace("\o" ,">",$new_line); 
    $new_line .= "\n";

    //link de imagens
    $new_line = str_replace("INCLUDEPICTURE" ,"<br><img src=",$new_line); 
    $new_line = str_replace("\*" ,"><br>",$new_line); 
    $new_line = str_replace("MERGEFORMATINET" ,"",$new_line); 

    $new_line = @iconv('UTF-8', 'ISO-8859-9', $new_line);
    $new_line = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", ' ', $new_line);
    $new_line = mb_strtolower(trim($new_line, '-'),'UTF-8');
    $new_line = preg_replace("/[\/_|+ -]+/", " ", $new_line);
    $new_line = preg_replace("/[0]/", "i", $new_line);
    $new_line = preg_replace("/[1]/", "i", $new_line);
    $outtext .= $new_line;
} return $outtext;} 

And here is 'echo $file' print. (Because of this print is very big, I post screen shot) enter link description here

0

I solved the problem. "@preg_match_all("/\b(([a-z0-9]+))\b/", $file, $fileOnlyAlphabetic);" when I write this code I get correct result. Thanks for your answer. I am so happy :)