1

I've been working around a long time trying to solve this problem, but no success until now.

I need to grab a string that is passed to a Keywork when saving a CDR file, but as far as I've tried, I have just failed!

I can't read the substring even it being displayed in the webbrowser.

The string to search for this test is "TESTKEYWORD", and below is my code:

<?php
$filename = "test.cdr";
$content = file_get_contents($filename);

if( strpos( $content, 'TESTKEYWORD' ) !== false ) {
    echo "I found the TESTKEYWORD string";
} else {
    echo "Sorry, I failed to find theTESTKEYWORD string!";
}

echo '<h2>Genarated string</h2>';
echo '<hr />';
echo $content;

If you want to try, I've uploaded in my webserver for testing, so you can access from there.

http://liuitt.com/cdr

Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
  • If your file is utf-8 encoded, you should look here http://stackoverflow.com/questions/15050710/php-strpos-substr-with-utf-8 – Henrique Barcelos May 17 '13 at 13:48
  • @HenriqueBarcelos, when I test to detect the string encoding, it returned NULL – Gilberto Albino May 17 '13 at 13:52
  • http://php.net/manual/en/function.mb-strpos.php – Henrique Barcelos May 17 '13 at 13:58
  • The string in the file is indeed unicode-encoded as suggested by Henrique. Make sure the search string has the same encoding as the string in the file. (Probably UTF-16 LE <-> BE) And use mb_strpos. – Adder May 17 '13 at 14:08
  • So, @Adder, I changed the code to find the encondings, the file content is UTF-8, and the string is ASCII. Is there a way to convert ASCII TO UTF-8? I tried $string = iconv('ASCII', 'UTF-8//TRANSLIT', $str); but it still returns ASCII – Gilberto Albino May 17 '13 at 16:46

1 Answers1

0

I think best way is to do this is using preg_match() command, try following code

<?php
$filename = "test.cdr";
$content = file_get_contents($filename);

$reex = "/TESTKEYWORD/";

if( preg_match($reex, $content, $matches)) {
    var_dump($matches);
    echo "I found the TESTKEYWORD string";
} else {
    echo "Sorry, I failed to find theTESTKEYWORD string!";
}
Thanu
  • 2,481
  • 8
  • 34
  • 53
  • I tested the code you shared, but it returns true even for anything I pass to the regular expression... – Gilberto Albino May 29 '13 at 14:13
  • Sorry that I couldn't do proper testing before posting the answer, I reckon the issue is that your content got special characters in it. May be you can have a look at "preg_quote" function to escape them, and then do the preg_match. Hope this would help. – Thanu May 30 '13 at 00:36