Jump to content

Code validation


ricklach

Recommended Posts

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
Share on other sites

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

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