msoutopico Posted December 3, 2013 Share Posted December 3, 2013 Hi there, I'm struggling a bit with this. This works fine: line 99: preg_match('^\s', $xmlElem) However, I shouldn't be writing the pattern there directly, because the pattern is in a tab-separated file. If I get it with fgetcsv and put it in another variable $pattern = $data[0]; echo $pattern; // outputs ^\s and then use that variable in the above match line 99: preg_match('/'. $pattern .'/', $xmlElem) then I get an error like: Warning: preg_match() [function.preg-match]: No ending delimiter '/' found in/Users/souto/Sites/path/to/file.php on line 99 and there's no match. Could anyone help me out? Thank you so much! Cheers, Manuel Quote Link to comment https://forums.phpfreaks.com/topic/284485-pattern-for-preg_match-comes-from-a-csv-file/ Share on other sites More sharing options...
.josh Posted December 3, 2013 Share Posted December 3, 2013 at face value, there is nothing wrong with the code you posted. Canned example: $fp = fopen('test.txt', 'w'); fputcsv($fp, array("^\s")); fclose($fp); $fp = fopen("test.txt", "r"); $data = fgetcsv($fp, 1000, ","); $pattern = $data[0]; $xmlElem = ' foobar'; echo $pattern; // outputs ^\s echo preg_match('/'. $pattern .'/', $xmlElem); output: ^\s 1 There must be more to your code than this.. What I do find odd is how you say this works: preg_match('^\s', $xmlElem) - when you don't have a delimiter in that pattern.. Quote Link to comment https://forums.phpfreaks.com/topic/284485-pattern-for-preg_match-comes-from-a-csv-file/#findComment-1461122 Share on other sites More sharing options...
msoutopico Posted December 4, 2013 Author Share Posted December 4, 2013 Hi Josh, Thank you very much for your kind answer. It turns out that my code was okay, the problem was the encoding of the file. I was using UTF-16 Little Endian, because it will need to contain text in double-byte languages (Asian languages, etc.). I tried changing it to ASCII and the script worked. So now I guess I need to find a way for my script to read double-byte stuff. Thanks a lot! Cheers, Manuel Quote Link to comment https://forums.phpfreaks.com/topic/284485-pattern-for-preg_match-comes-from-a-csv-file/#findComment-1461200 Share on other sites More sharing options...
.josh Posted December 4, 2013 Share Posted December 4, 2013 Well tbh I don't have much experience working with double-byte languages (never really been needed for my job) but I do know that for the regex you can use the u modifier to put the regex engine in utf8 mode. There isn't a utf-16 version though (that I'm aware of) .. As far as reading the file, here is a comment in fgetcsv entry that might help. Actually, this comment might be more useful to you, since it mentions utf-16 specifically. Quote Link to comment https://forums.phpfreaks.com/topic/284485-pattern-for-preg_match-comes-from-a-csv-file/#findComment-1461252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.