Jump to content

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.

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.