Jump to content

Pattern for preg_match comes from a CSV file


Recommended Posts

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

Link to comment
Share on other sites

at face value, there is nothing wrong with the code you posted. Canned example:


$fp = fopen('test.txt', 'w');
fputcsv($fp, array("^\s"));

$fp = fopen("test.txt", "r");
$data = fgetcsv($fp, 1000, ",");

$pattern = $data[0];
$xmlElem = ' foobar';

echo $pattern; // outputs ^\s
echo preg_match('/'. $pattern .'/', $xmlElem);

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.