ricklach Posted November 23, 2007 Share Posted November 23, 2007 If I have the line: 1 SOUR TMG in a text file in variable $fcontents, will the following code produce a "true" value? $ct = preg_match("/SOUR.+(TMG|The Master Genealogist)/", $fcontents); if ($ct == 0) return false; return true; or do I need a space in the preg_match statement like this "/SOUR .+(...) Rick Link to comment https://forums.phpfreaks.com/topic/78520-code-validation/ Share on other sites More sharing options...
BenInBlack Posted November 23, 2007 Share Posted November 23, 2007 that regex will match and so true is a correct response. it matches because the preg_match using that regex does find "SOUR TMG" in the whole of the contents if you add a space, yes it will return false but for the wrong reason, the space is greedy which then puts the regex pointer on the S of SOUR and now the .+ will move you to the end of the content, basically ignoring the rest of the regex statement. your are going to have to split the $fcontents into line and change the regex to something like so each line is passed thru it one at a time so your var $fcontents = '1 SOUR TMG' and adding the caret to the regex states the pattern start at position 1 of the content /^SOUR .+(TMG|The Master Genealogist)/ Link to comment https://forums.phpfreaks.com/topic/78520-code-validation/#findComment-397358 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.