Jump to content


Copy To Variable


9 replies to this topic

#1 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 20 August 2007 - 01:30 PM

If i were to put something like [link]www.something.com[/link] how would i put whatever is between the tags into a variable.

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 20 August 2007 - 02:38 PM

Regex.

<?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 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 20 August 2007 - 02:47 PM

thanks

#4 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 20 August 2007 - 02:53 PM

but that didnt work
i just looking to store whatever is inbetween those tags in a variable.

#5 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 20 August 2007 - 03:43 PM

$var = $results[1];

?

#6 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 20 August 2007 - 08:47 PM

it didnt work.

#7 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

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

#8 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

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 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

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 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 21 August 2007 - 04:19 PM

View PostHip 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.

View PostDemonslay, 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