scarhand Posted November 6, 2008 Share Posted November 6, 2008 lets say i have a paragraph like this: rat pig bear rat pig bear rat pig bear rat cat dog rat pig bear rat pig bear rat cat monkey rat pig bear rat pig bear i am trying to get "dog" and "monkey" from the preg_match array.... heres my code which doesnt work: <?php preg_match('/cat (.+)$(.*)cat (.+)$/', $paragraph, $matches); $animal1 = $matches[1]; $animal2 = $matches[2]; ?> im a total regex newb :-\ Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/ Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 figured out how to ge tthe first one with this: preg_match('$cat (.*)$', $paragraph, $matches); $animal1 = $matches[1]; but how do i get the second one? Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684003 Share on other sites More sharing options...
effigy Posted November 6, 2008 Share Posted November 6, 2008 <pre> <?php $data = <<<DATA rat pig bear rat pig bear rat pig bear rat cat dog rat pig bear rat pig bear rat cat monkey rat pig bear rat pig bear DATA; preg_match_all('/(?<=\scat\s).+$/m', $data, $matches); print_r($matches); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684017 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 thanks, wasnt aware of preg_match_all one more thing, i am trying to take content from old static pages and insert them into a database now i am trying to get the content (which can be longer than 1 line) between each font tag so i can insert it into my database the code i am using for that isnt working either: <?php preg_match_all('/<font style="font-size: 14px;">(.*)</font>/', $oldpage, $bmatches); foreach ($bmatches as $value) { echo $value; } Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684028 Share on other sites More sharing options...
effigy Posted November 6, 2008 Share Posted November 6, 2008 print_r the array to see what you're working with. $bmatches is an array of arrays. Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684042 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 i get an unkown modifier error Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684060 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 i figured that out but the array contains nothing the code looks like this: <font style="font-size: 14px;"> there once was a man who lived on a farm </font> heres my php code: <?php preg_match_all('/<font style="font-size: 14px;">(.*)<\/font>/', $page, $bmatches); foreach ($bmatches as $value) { echo $value; } ?> Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684094 Share on other sites More sharing options...
effigy Posted November 6, 2008 Share Posted November 6, 2008 . will not match new lines without the /s modifier. Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684106 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 thank you SO MUCH i am in the process of reading your regexp resource article as well Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684115 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 ugh i have one more problem now the page has more than 1 instance of what im trying to get content from example: <font style="font-size: 14px;"> there once was a man who lived on a farm </font> <font style="font-size: 14px;"> there once was a man who lived on a farm </font> <font style="font-size: 14px;"> there once was a man who lived on a farm </font> my array is outputting this: there once was a man who lived on a farm </font> <font style="font-size: 14px;"> there once was a man who lived on a farm </font> <font style="font-size: 14px;"> there once was a man who lived on a farm heres my php code: <?php preg_match_all('/<font style="font-size: 14px;">(.*)<\/font>/s', $page, $bmatches); echo $bmatches[1][0]; ?> Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684124 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 nevermind i fixed it by adding a ? Link to comment https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/#findComment-684128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.