plznty Posted January 5, 2011 Share Posted January 5, 2011 How can I preg_match the alt value for each of these? Thanks in advance. <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/7.bmp" alt="7" /></li> <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/4.bmp" alt="4" /></li> <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/0.bmp" alt="0" /></li> Quote Link to comment https://forums.phpfreaks.com/topic/223497-preg_match/ Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 $tag = "<img src=\"yes/0.bmp\" alt=\"1\" />"; $matches = array(); echo preg_match("/alt=\"[0-9]{1,2}\"/", $tag, $matches); echo '<pre>'; print_r($matches) echo '</pre>'; That worked for me, only registers alts that are in the range of 1-2 characters, ie up to 99, though that can easily be changed in the expression. Returns: alt="1" Quote Link to comment https://forums.phpfreaks.com/topic/223497-preg_match/#findComment-1155296 Share on other sites More sharing options...
Anti-Moronic Posted January 5, 2011 Share Posted January 5, 2011 Another way would be this: $html = ' <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/7.bmp" alt="7" /></li> <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/4.bmp" alt="4" /></li> <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/0.bmp" alt="0" /></li> <li><img src="yes/0.bmp" alt="0" /></li> '; preg_match_all("/alt=\"(.*?)\"/", $html, $matches); var_dump($matches); ..which outputs... array 0 => array 0 => string 'alt="0"' (length=7) 1 => string 'alt="7"' (length=7) 2 => string 'alt="0"' (length=7) 3 => string 'alt="4"' (length=7) 4 => string 'alt="0"' (length=7) 5 => string 'alt="0"' (length=7) 6 => string 'alt="0"' (length=7) 1 => array 0 => string '0' (length=1) 1 => string '7' (length=1) 2 => string '0' (length=1) 3 => string '4' (length=1) 4 => string '0' (length=1) 5 => string '0' (length=1) 6 => string '0' (length=1) Quote Link to comment https://forums.phpfreaks.com/topic/223497-preg_match/#findComment-1155308 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.