Jump to content

Extracting all instances of [DOCLINK=numberhere] from string and...


matt.xx

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/>';
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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).

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.