Joeker Posted August 20, 2008 Share Posted August 20, 2008 I have a page's content which is formatted as follows: Name: Joe Blow Job: Construction Worker Hello my name is joe and I work in construction. I want to get the name and the job in its own variables. Here is my code so far, which does not work 100%: <?php $matches1 = array(); $pos = strpos($content,"\n"); $name = substr($content, 0, $pos); preg_match('$Name: ([a-z ]+)$i', $name, $matches1); $name = $matches1[1]; $matches2 = array(); $second = substr($content, $pos); $pos2 = strpos($second,"\n"); $job = substr($second, 0, $pos2); preg_match('$Job: ([a-z ]+)$i', $job, $matches2); $job = $matches2[1]; ?> Anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/120542-getting-2-values-from-2-lines/ Share on other sites More sharing options...
JonnoTheDev Posted August 20, 2008 Share Posted August 20, 2008 This will not find uppercase characters. You can either convert the string to lowercase or adjust the regex: ([A-Za-z ]+) You may also need to include extra characters as names may appear like John O'Reilly Quote Link to comment https://forums.phpfreaks.com/topic/120542-getting-2-values-from-2-lines/#findComment-621164 Share on other sites More sharing options...
Joeker Posted August 20, 2008 Author Share Posted August 20, 2008 This will not find uppercase characters. You can either convert the string to lowercase or adjust the regex: ([A-Za-z ]+) You may also need to include extra characters as names may appear like John O'Reilly Quotes are stripped, and as for the case sensitivity: thats what the $i is for. Quote Link to comment https://forums.phpfreaks.com/topic/120542-getting-2-values-from-2-lines/#findComment-621172 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.