matfish Posted March 4, 2010 Share Posted March 4, 2010 Hi there, I have HTML code which I then strip_tag just to get plain text into $text variable. The text looks like: $text = "This paragraph references to [code: ref1, ref3, ref87] and the next bit references to [code: ref9, ref4]. There could be several references like [code: ref67, ref55]." I want to put all those code: ref numbers into an array? I tried something, but after it wrote the first ref numbers to an array it quit and does not look at the rest? Any ideas would be appreciated. Link to comment https://forums.phpfreaks.com/topic/194138-string-to-array/ Share on other sites More sharing options...
jskywalker Posted March 4, 2010 Share Posted March 4, 2010 I tried something, but after it wrote the first ref numbers to an array it quit and does not look at the rest? i also tried something, but after i write the first couple of lines, it quit, and did not report a correct answer..... But...., i tried again: <?php $text = "This paragraph references to [code: ref1, ref3, ref87] and the next bit references to [code: ref9, ref4]. There could be several references like [code: ref67, ref55]."; $s=preg_split("/\[code:/", $text); echo $text."<hr>"; print_r($s); print "<hr>"; for ($f=0; $f<count($s); $f++) { print "$f: $s[$f] <br>\n"; } print "<hr>"; $ref=""; for ($f=1; $f<count($s); $f++) { $t=substr($s[$f],0,strpos($s[$f],']')); $ref=$ref.$t.','; print "$f: $t <br>\n"; } $b=explode(",",$ref); print "<hr>"; for ($f=0; $f<count($b); $f++) { print "$f: $b[$f] <br>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/194138-string-to-array/#findComment-1021484 Share on other sites More sharing options...
matfish Posted March 4, 2010 Author Share Posted March 4, 2010 Works a treat. Thanks for the speedy help. Link to comment https://forums.phpfreaks.com/topic/194138-string-to-array/#findComment-1021505 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.