Jump to content

Getting 2 values from 2 lines


Joeker

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/120542-getting-2-values-from-2-lines/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

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