If i were to put something like [link]www.something.com[/link] how would i put whatever is between the tags into a variable.
Copy To Variable
Started by Hip Hop Artist, Aug 20 2007 01:30 PM
9 replies to this topic
#1
Posted 20 August 2007 - 01:30 PM
#2
Posted 20 August 2007 - 02:38 PM
Regex.
$results[0] will contain an array of the full matches, and $results[1] will contain an array of the text between the tags.
<?php
$text = '[link]Text here[/link][link]Text number twp[/link]';
preg_match_all('#\[link\]([^\[]+)\[/link\]#', $text, $results);
var_dump($results);
?>
$results[0] will contain an array of the full matches, and $results[1] will contain an array of the text between the tags.
#3
Posted 20 August 2007 - 02:47 PM
thanks
#4
Posted 20 August 2007 - 02:53 PM
but that didnt work
i just looking to store whatever is inbetween those tags in a variable.
i just looking to store whatever is inbetween those tags in a variable.
#5
Posted 20 August 2007 - 03:43 PM
$var = $results[1];
?
#6
Posted 20 August 2007 - 08:47 PM
it didnt work.
#7
Posted 20 August 2007 - 08:57 PM
Then what did it do...
C'mon, you can't be so vague with your results.
Use var_dump() or print_r() on the result set and you'll what it looks like. As I said, the second index (or in arrays terms, $result[1]) will be all matches of what is captured between those tags. It will come with as many results as it finds, so in the example I gave you, $result[1][0] will read 'Text here', and $result[1][1] will read 'Text number twp' (noticed my typo there, lol).
C'mon, you can't be so vague with your results.
Use var_dump() or print_r() on the result set and you'll what it looks like. As I said, the second index (or in arrays terms, $result[1]) will be all matches of what is captured between those tags. It will come with as many results as it finds, so in the example I gave you, $result[1][0] will read 'Text here', and $result[1][1] will read 'Text number twp' (noticed my typo there, lol).
#8
Posted 20 August 2007 - 09:04 PM
ok well i didnt know it had to be $result[1][0] you just said to put $result[1]but this is for a forum so how would i know how many links they put......i wouldnt know how many arrays to echo or whatever.......
#9
Posted 21 August 2007 - 02:01 PM
All the results you want will be in $result[1]. Just loop through that array or call count($result[1]) and that will return how many there are.
#10
Posted 21 August 2007 - 04:19 PM
Hip Hop Artist, on Aug 20 2007, 09:04 PM, said:
ok well i didnt know it had to be $result[1][0] you just said to put $result[1]
Um, no I didn't.
Demonslay, on Aug 20 2007, 02:38 PM, said:
$results[0] will contain an array of the full matches, and $results[1] will contain an array of the text between the tags.
As bolded above, I told you it could contain an array of the results.
And an alternative to rc's suggestion of using count() on the result array, you can also capture the return from preg_match_all() into a variable, as it returns the number of results it matched.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
