Jump to content

[SOLVED] preg_match_all pattern question


dtdetu

Recommended Posts

PHP Code Example:
<?php
$sourcestring="your source string";
preg_match_all('~<td><a href="profile\.php\?[^"]*"><span style="color:#00FF00;">([^<]*)</span></a></td>~',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

$matches Array:
(
    [0] => Array
        (
            [0] => <td><a href="profile.php?x=26&y=1&selected=ashland"><span style="color:#00FF00;">ashland</span></a></td> 
        )

    [1] => Array
        (
            [0] => ashland
        )

)

or:

PHP Code Example:
<?php
$sourcestring="your source string";
preg_match_all('~<td><a href="profile\.php\?x=26&y=1&selected=([^"]*)"><span style="color:#00FF00;">[^<]*</span></a></td>~',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

$matches Array:
(
    [0] => Array
        (
            [0] => <td><a href="profile.php?x=26&y=1&selected=ashland"><span style="color:#00FF00;">ashland</span></a></td> 
        )

    [1] => Array
        (
            [0] => ashland
        )

)

Since it depends which "ashland" you wanted.

hey ddrudik thanks for the code it works as i want (the first one), but also the color value changes in everytime like x and y so what is the new pattern, also how can i get ashland without an array , i mean that outputs array , how to make it print only ashland, thanks

Perhaps I am missing something.. but can you not simply grab ashland so long as it isn't accompanied by say an equal sign?

 

$str = '<td><a href="profile.php?x=26&y=1&selected=ashland"><span style="color:#00FF00;">ashland</span></a></td> ';
preg_match('#((?<!=)ashland)#', $str, $match);
echo $match[1];

 

Output:

ashland

 

Dedetu, you only need to use preg_match_all if there is more than one instance of something you want to find.. So I am not sure if my provided example will suffice or not...

 

Cheers,

 

NRG

The string could also be:

<td><a href="profile.php?x=26&y=1&selected=foo"><span style="color:#00FF00;">foo</span></a></td> 

Or:

<td><a href="profile.php?x=26&y=1&selected=bar"><span style="color:#00FF00;">bar</span></a></td> 

 

Get it? >_<

yes it can be anything foo,bar,ashland or dtdetu below code is working but i want the color to be a variable too, it shouldnt be #00FF00; bec it changes also so we need to change the pattern again. but how:)

 

<?php
$sourcestring="your source string";
preg_match_all('~<td><a href="profile\.php\?[^"]*"><span style="color:#00FF00;">([^<]*)</span></a></td>~',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

 

<?php
$sourcestring = file_get_contents("state.htm");

preg_match_all('~<td><a href="profile\.php\?[^"]*"><span style="color:#[^;]+;">([^<]*)</span></a></td>~',$sourcestring,$matches);
echo $matches[1][4];
echo "\n";



?>

now when i search a page for my pattern it finds 7 instances, then i can see them with the

$matches[1][1],$matches[1][2].... but how can i see them all, i used for but couldnt make it work.

 

$i = 0;

foreach ($matches as $val) 
{
$amount = count($val);
while($i < $amount)
{	
   echo $val[1][$i]);
	echo "\n";
	$count[] += $i;
	$i ++;
}
}

 

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.