dtdetu Posted November 11, 2008 Share Posted November 11, 2008 hello i want to get ashland with preg_match_all function from the below code but couldnt find the right pattern to use , can anyone check this. <td><a href="profile.php?x=26&y=1&selected=ashland"><span style="color:#00FF00;">ashland</span></a></td> Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/ Share on other sites More sharing options...
ddrudik Posted November 11, 2008 Share Posted November 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-687855 Share on other sites More sharing options...
sasa Posted November 11, 2008 Share Posted November 11, 2008 '/<a href="profile\.php\?[^"]*selected=([^"&]*)["&][^>]+><span[^>]+>([^<]*)</' Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-687858 Share on other sites More sharing options...
dtdetu Posted November 11, 2008 Author Share Posted November 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688041 Share on other sites More sharing options...
nrg_alpha Posted November 11, 2008 Share Posted November 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688124 Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 I think he wants thinks that could be in the spot where ashland is...otherwise: $name = "ashland"; Would do the same thing. Right? Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688131 Share on other sites More sharing options...
nrg_alpha Posted November 11, 2008 Share Posted November 11, 2008 I'm confused now.. perhaps I'm not understanding something here lol Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688132 Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 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? >_< Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688136 Share on other sites More sharing options...
dtdetu Posted November 11, 2008 Author Share Posted November 11, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688138 Share on other sites More sharing options...
nrg_alpha Posted November 11, 2008 Share Posted November 11, 2008 preg_match_all('~<td><a href="profile\.php\?[^"]*"><span style="color:#[^;]+;">([^<]*)</span></a></td>~',$sourcestring,$matches); Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688144 Share on other sites More sharing options...
dtdetu Posted November 11, 2008 Author Share Posted November 11, 2008 thanks nrg_alpha thats what i wanteded Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688145 Share on other sites More sharing options...
dtdetu Posted November 12, 2008 Author Share Posted November 12, 2008 <?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 ++; } } Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688150 Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 foreach($matches[1] as $value) { echo $value . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688151 Share on other sites More sharing options...
dtdetu Posted November 12, 2008 Author Share Posted November 12, 2008 thanks again you are great Quote Link to comment https://forums.phpfreaks.com/topic/132302-solved-preg_match_all-pattern-question/#findComment-688153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.