Page 1 of 1

Problems using RegExps

Posted: Thu Jul 29, 2004 4:27 am
by visionmaster
Hello,

I would like to parse a string and have problems using RegExps. The ouput of the following code snippet is bellow.
I just want everything behind each descr in an array like this:

Code: Select all

array(10) {
  ї0]=>
  string(0) "SPIEGEL ONLINE GmbH"
  ї1]=>
  string(0) "Brandstwiete 19"
  ї2]=>
  string(0) "20457 Hamburg"
  ї3]=>
  string(0) "Germany"
}
==> What is wrong in my script, since I'm getting empty strings? O.k., I guess my regexp is wrong... :(

Code: Select all

[...]
$output = extractBlocks($rawoutput,'domain:','source:');
echo $output;
//Parse string with RegExps
$arrData = getDataUsingRegexp('|:(.*)\s|U',$output);  		
goDumpArray($arrData);
[...]					

function getDataUsingRegexp($strRegexp,$string)
{               
	preg_match_all($strRegexp, $string, $matches);
		
	$arrListe = array();	
		
	for ($i=0; $i< count($matches[0]); $i++)
	{   
	   $strData = trim($matches[1][$i]);      	     	    
	     
	   $arrListe[] = $strData;        	               
	}
	   
	return $arrListe;
	
}


Input:
---------

Code: Select all

domain:    spiegel.de
descr:       SPIEGEL ONLINE GmbH
descr:       Brandstwiete 19
descr:       20457 Hamburg
descr:       Germany
nserver:    igate.spiegel.de 194.64.251.5
nserver:    dns-s.is-europe.net
nserver:    sec-s.is-europe.net
status:       connect
changed:    2003-08-16T05:05:38+0200
source:      DENIC
Output:
---------

Code: Select all

spiegel.de
descr:       SPIEGEL ONLINE GmbH
descr:       Brandstwiete 19
descr:       20457 Hamburg
descr:       Germany
nserver:     igate.spiegel.de 194.64.251.5
nserver:     dns-s.is-europe.net
nserver:     sec-s.is-europe.net
status:      connect
changed:     2003-08-16T05:05:38+0200
array(10) &#123;
  &#1111;0]=&gt;
  string(0) ""
  &#1111;1]=&gt;
  string(0) ""
  &#1111;2]=&gt;
  string(0) ""
  &#1111;3]=&gt;
  string(0) ""
  &#1111;4]=&gt;
  string(0) ""
  &#1111;5]=&gt;
  string(0) ""
  &#1111;6]=&gt;
  string(0) ""
  &#1111;7]=&gt;
  string(0) ""
  &#1111;8]=&gt;
  string(0) ""
  &#1111;9]=&gt;
  string(10) "05:38+0200"
&#125;

feyd | added

Code: Select all

tags around I/O[/color]
[color=red][b]Bech100[/b] | Disabled smilies cos they were screwing up the php[/color]

Posted: Thu Jul 29, 2004 4:43 am
by feyd

Code: Select all

<?php

$input = 'domain:    spiegel.de
descr:       SPIEGEL ONLINE GmbH
descr:       Brandstwiete 19
descr:       20457 Hamburg
descr:       Germany
nserver:    igate.spiegel.de 194.64.251.5
nserver:    dns-s.is-europe.net
nserver:    sec-s.is-europe.net
status:       connect
changed:    2003-08-16T05:05:38+0200
source:      DENIC';

function getDataUsingRegexp($strRegexp,$string)
{               
   preg_match_all($strRegexp, $string, $matches);
      
   $arrListe = array();   
      
   for ($i=0; $i< count($matches[0]); $i++)
   {   
      $strData = trim($matches[1][$i]);                    
        
      $arrListe[] = $strData;                          
   }
      
   return $arrListe;
   
}

$output = getDataUsingRegexp('#^[^:]*:\s*(.*?)\s*$#m',$input);

echo $input."\n";
var_dump($output);

?>
outputs

Code: Select all

domain:    spiegel.de
descr:       SPIEGEL ONLINE GmbH
descr:       Brandstwiete 19
descr:       20457 Hamburg
descr:       Germany
nserver:    igate.spiegel.de 194.64.251.5
nserver:    dns-s.is-europe.net
nserver:    sec-s.is-europe.net
status:       connect
changed:    2003-08-16T05:05:38+0200
source:      DENIC
array(11) &#123;
  &#1111;0]=&gt;
  string(10) "spiegel.de"
  &#1111;1]=&gt;
  string(19) "SPIEGEL ONLINE GmbH"
  &#1111;2]=&gt;
  string(15) "Brandstwiete 19"
  &#1111;3]=&gt;
  string(13) "20457 Hamburg"
  &#1111;4]=&gt;
  string(7) "Germany"
  &#1111;5]=&gt;
  string(29) "igate.spiegel.de 194.64.251.5"
  &#1111;6]=&gt;
  string(19) "dns-s.is-europe.net"
  &#1111;7]=&gt;
  string(19) "sec-s.is-europe.net"
  &#1111;8]=&gt;
  string(7) "connect"
  &#1111;9]=&gt;
  string(24) "2003-08-16T05:05:38+0200"
  &#1111;10]=&gt;
  string(5) "DENIC"
&#125;

Posted: Thu Jul 29, 2004 5:00 am
by visionmaster
Hi,

Thanks for your solution! But I really just want an array with everthing behind descr:, discarding everything else. Like this:

array(11) {
[0]=>
string(19) "SPIEGEL ONLINE GmbH"
[1]=>
string(15) "Brandstwiete 19"
[2]=>
string(13) "20457 Hamburg"
[3]=>
string(7) "Germany"
}