Jump to content

Recommended Posts

i have a problem

 

for example i want to use eregi to retrive data

 

i want to retrive data from an website

the code from the page looks like this

 

<li id="cote">23:55</li>
<li id="cote">1.40</li>
<li id="cote">
4.00
</li>
<li id="cote">
1.04    
</li>
<li id="cote">
2.49    
</li>
<li id="cote">
1.15    
</li>

 

if i use

 

if (eregi('<li id="cote">(.*)</li>', $line, $out)) {
      $categ = each ($out);
                 echo $categ[1]."\n";
}

 

the result is

<li id="cote">23:55</li>

<li id="cote">1.40</li>

 

i retrive only a part of the code becouse are whitespaces or new lines between

 

<li id="cote">(.*)</li>

 

how to use eregi... to retrive all the data ?

becouse works only this part

 

<li id="cote">23:55</li>

<li id="cote">1.40</li>

 

and i lose this part

 

<li id="cote">

4.00

</li>

<li id="cote">

1.04   

</li>

<li id="cote">

2.49   

</li>

<li id="cote">

1.15   

</li>

 

 

Link to comment
https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/
Share on other sites

$line is probably one line of the file. You can't use that here as some parts are on new lines. You can use eregi() to retrieve all the data from the file without having to use file().

 

$contents = file_get_contents('path/to/file');
$matches = array();
preg_match('/\d{1,2}(:|.)\d{1,2}/', $contents, $matches);
print_r($matches);

if i use youre code

 

<?php 

$contents ='
<li id="cote">23:55</li>
<li id="cote">1.40</li>
<li id="cote">
4.00
</li>
<li id="cote">
1.04    
</li>
<li id="cote">
2.49    
</li>
<li id="cote">
1.15    
</li>
';
$matches = array();
preg_match('/\d{1,2}(:|.)\d{1,2}/', $contents, $matches);
print_r($matches);

//the result is
?>

 

Array ( [0] => 23:55 [1] => : ) 

 

and i lose:

1.40

4.00

1.04   

2.49   

1.15

Thaks

it`s working

 

 

i want you to ask...if i want to retrieve some complex data

like strings and numbers in same time

its possible to do this thing?

 

for example

 

<?php

$contents ='
<li id="cote">8655</li>
<li id="echipe">
Flamengo : Cruzeiro  
</li>
<li id="cote">23:55</li>
<li id="cote">
2.00 
</li>
<li id="cote">
3.25 
</li>
<li id="cote">
3.25    
</li>
<li id="cote">
1.24    
</li>
<li id="cote">
1.63    
</li>
<li id="cote">
1.24    
</li>
';
?>

 

and result to be:

 

8655

Flamengo : Cruzeiro

23:55

2.00

3.25

3.25   

1.24   

1.63   

1.24 

$matches = array();
$contents = file_get_contents('path/to/file');
preg_match_all('/\d{1,2}(:|\.)\d{1,2}/', $contents, $matches);
print_r($matches);

 

Sorry wrong function :)

 

$matches = array();
$contents = file_get_contents('path/to/file');
preg_match_all('/([a-z0-9]+(:|\.)[a-z0-9]+)/i', $contents, $matches);
print_r($matches);

 

May work but will not match 8655 and Flamengo : Cruzeiro if they contain a space before and after :.

If I understand correctly, would something like this work for you?

 

$html = <<<EOF
<li id="cote">8655</li>
<li id="echipe">
Flamengo : Cruzeiro
</li>
<li id="cote">23:55</li>
<li id="cote">
2.00
</li>
<li id="cote">
3.25
</li>
<li id="cote">
3.25
</li>
<li id="cote">
1.24
</li>
<li id="cote">
1.63
</li>
<li id="cote">
1.24
</li>
EOF;

preg_match_all('#<li[^>]*>([^<]+)</li>#', $html, $matches);
unset($matches[0]);
$matches[1] = array_map('trim', $matches[1]);
echo '<pre>'.print_r($matches[1], true);

Topic CLOSED

 

Actually, to close a topic, you flag it solved:) Oh, and just for future reference, I wouldn't use ereg anymore.. as of the current stable PHP release (5.3), it is already depreciated... you should learn pcre instead, as posix (which ereg come from) will no longer be included within the core of PHP as of version 6.

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.