SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Turn SOME lines from a string into an array via regex???

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Turn SOME lines from a string into an array via regex???

    Hi guys,

    I have been working on this for 4 hours now and cannot get my head around it (I know there is a simple solution).

    I have a script running which returns this content:

    Code:
    ---
    FieldType: Text
    FieldName: user_email
    FieldFlags: 12582912
    FieldJustification: Right
    ---
    FieldType: Text
    FieldName: user_phone
    FieldFlags: 12582912
    FieldJustification: Right
    ---
    FieldType: Text
    FieldName: user_url
    FieldFlags: 12582912
    FieldJustification: Right
    How would I go about returning an array of just the FieldNames (the bit after the colons) without the ---, FieldType, FieldFlags or FieldJustification???

    Desired output from the example above:
    Code:
    Array
    (
        [0] => user_email
        [1] => user_phone
        [2] => user_url
    )
    Thank you very much for your time, many thanks,

    Andy

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $p '#FieldName: (.+)\s#';
    preg_match_all($p$text$m);
    print_r($m[1]); 

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Legend!

    Thank you!

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,360
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    PHP Code:
    $p '#FieldName: (.+)\s#'
    I'd probably go more for /^FieldName: (.+)$/m

    It just seems a little more logical to me, given the source data and what's wanted. Of course, there are many ways to skin this cat.
    Salathe
    Software Developer and PHP Documentation Team.

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    914
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if thats what you were looking for, try this:
    Code:
    $str = <<<EOD
    ---
    FieldType: Text
    FieldName: user_email
    FieldFlags: 12582912
    FieldJustification: Right
    ---
    FieldType: Text
    FieldName: user_phone
    FieldFlags: 12582912
    FieldJustification: Right
    ---
    FieldType: Text
    FieldName: user_url
    FieldFlags: 12582912
    FieldJustification: Right
    EOD;
    
    $arrFields = preg_split("/---/", $str);
    
    $aResult = array();
    for($i=0; $i<count($arrFields); $i+=1){
    	$Val = $arrFields[$i];
    	if(!empty($Val)){
    		
    		$arr = explode("\n", $Val);
    		foreach($arr as $line){
    			if(!empty($line)){
    				$aLine = explode(":", $line);
    				if(array_key_exists('1', $aLine)){
    					echo __LINE__.' aLine: '.print_r($aLine, true)."\n";
    					$arr[] = $aLine[1];
    				}
    
    				$aResult[$i][] = $arr;
    			}
    		}
    	}
    }
    
    
    print_r($aResult);

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys,

    Thank you for your responses.

    crmalibu's solution works perfectly.

    @Salathe: Could you explain why your regex is better, so that I can choose which one to use in the final implementation?

    Again, many thanks guys

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,360
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Sure, it just takes a slightly different approach to crmalibu's pattern. Mine checks for [i]start of a line followed by "Fieldname: " followed by something (not a newline) followed by the end of a line[i] whereas crmalibu's checks for "Fieldname: " (not necessarily at the start of a line) followed by something (not a newline) followed by a whitespace character (probably a newline, could be a tab or space…). I also didn't say that it was better, just different. Mine is a little more precisely focussed on your content sample in the first post.
    Salathe
    Software Developer and PHP Documentation Team.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •