Jump to content

String to array


matfish

Recommended Posts

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

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.....  :shrug:

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.