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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

 

<?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 ++;
}
}

 

Link to comment
Share on other sites

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.