matt.xx Posted December 2, 2009 Share Posted December 2, 2009 Hi all, i'm new to these forums but i came across them looking for help! I've been having a little bit of trouble trying to do this, as stated above.. i basically just want to extract all instances of the doclink tags within a given string and once i've done that, to go one step further and extract the number from the extracted doclink tag.. if that makes any sense? I'm fairly new to php and i couldn't really find a half decent way around this, i don't understand the pregmatch function with all of the symbols to define?! this is what i have so far, please don't laugh if it's the complete wrong way of doing it <?php $text = "fdsafdsf fdafdsf dafdsfdsa fdsafdsfdsafsda[DOCLINK=5] fdsafdsa fdsafdsf dsafdsafasfdsaf fdsaf"; $extracted = strstr($text, '[DOCLINK='); echo $extracted."<br />"; ?> I've got a feeling i'm going about this the complete wrong way, but could anybody please offer me any assistance? Many Thanks Matt Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 2, 2009 Share Posted December 2, 2009 hope this helps <?php $text = "fdsafdsf fdafdsf dafdsfdsa fdsafdsfdsafsda[DOCLINK=5] fdsafdsa fdsafdsf dsafdsafasfdsaf fdsaf"; preg_match("#\[DOCLINK=(\d+)\]#", $text, $matches); print_r($matches); ?> Quote Link to comment Share on other sites More sharing options...
matt.xx Posted December 2, 2009 Author Share Posted December 2, 2009 Thankyou for the quick response, that's brilliant mate! now my question to you is, how do i set up some form of loop, so that it detects every instance of doclink within a provided string (rather than just the one)? finally, how do i display them in the following format (as two seperate values): [DOCLINK=5] 5 so it splits them up, rather than keeping them within an array? The output of the provided code is as follows: Array ( [0] => [DOCLINK=5] [1] => 5 ) Thanks Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 2, 2009 Share Posted December 2, 2009 if its all in one string you can use preg_match_all instead of preg_match depends on your string can you give an example ? Quote Link to comment Share on other sites More sharing options...
cags Posted December 2, 2009 Share Posted December 2, 2009 Just use preg_match_all instead of preg_match. $matches is a two dimentional array. $matches[0] is an array containing all values that match your entire pattern and $matches[1] is an array that contains all matches from the first capture group (set of brackets, in your case the number). Just work with them like any other array. Example... foreach($matches[0] $k=>$v) { echo $v . '<br/>'; echo $matches[1][$k] . '<br/>'; } Quote Link to comment Share on other sites More sharing options...
matt.xx Posted December 2, 2009 Author Share Posted December 2, 2009 Thanks guys, that's absolutely brilliant. All of my problems solved in less than an hour, i'm extremely grateful! for those who are interested, here is the completed code: <?php $text = "fdsafdsf fdafdsf dafdsfdsa fdsafdsfdsafsda[DOCLINK=5] fdsafdsa fdsafdsf dsafdsafasfdsaf[DOCLINK=6] fdsaf [DOCLINK=99]"; preg_match_all("#\[DOCLINK=(\d+)\]#", $text, $matches); foreach($matches[0] as $k=>$v) { echo $v . '<br/>'; echo $matches[1][$k] . '<br/>'; } ?> and this is the output: [DOCLINK=5] 5 [DOCLINK=6] 6 [DOCLINK=99] 99 Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted December 4, 2009 Share Posted December 4, 2009 Alternatively, you could use the escape sequence \K to eliminate the need to capture altogether. Example: $text = "fdsafdsf fdafdsf dafdsfdsa fdsafdsfdsafsda[DOCLINK=5] fdsafdsa fdsafdsf dsafdsafasfdsaf[DOCLINK=6] fdsaf [DOCLINK=99]"; preg_match_all("#\[DOCLINK=\K\d+#", $text, $matches); foreach($matches[0] as $val) echo "[DOCLINK=$val]<br />\n$val<br />\n"; Since the pattern explicitly looks for [DOCLINK=, we really wouldn't need to include that in the match (nor the closing bracket). But, like many other things, I say toe-[may]-toe, you say toe-[mat]-toe. All the same either way I suppose (with the exception of the capture of course). Quote Link to comment 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.