edainworks.com :: VGR :: BBcode interpretation for output.

Run :
source data :
Array
(
    [0] => first line
    [1] => second line with a [url=http://www.site.com]text[/url] link
    [2] => third line
    [3] => fourth [url=http://www.site.com]link text2[/url] line with two of them [url=http://www.site.com]link text3[/url]
    [4] => fifth line
)
original : 'first line'
final : 'first line'
original : 'second line with a [url=http://www.site.com]text[/url] link'
text= 'text'
url= 'http://www.site.com'
rebuilt : 'second line with a <a href="http://www.site.com">text</A> link'
final : 'second line with a text link'
original : 'third line'
final : 'third line'
original : 'fourth [url=http://www.site.com]link text2[/url] line with two of them [url=http://www.site.com]link text3[/url]'
text= 'link text2'
url= 'http://www.site.com'
rebuilt : 'fourth <a href="http://www.site.com">link text2</A> line with two of them [url=http://www.site.com]link text3[/url]'
text= 'link text3'
url= 'http://www.site.com'
rebuilt : 'fourth <a href="http://www.site.com">link text2</A> line with two of them <a href="http://www.site.com">link text3</A>'
final : 'fourth link text2 line with two of them link text3'
original : 'fifth line'
final : 'fifth line'
result data :
Array
(
    [0] => first line
    [1] => second line with a text link
    [2] => third line
    [3] => fourth link text2 line with two of them link text3
    [4] => fifth line
)

Code :
//
// parseurls2.php
//
//VGR16062003 Création
//VGR21082009 ADDed code et enrobage
//

function AtomSubst($thestr, $DEBUG=FALSE) { // operates on a string
  if ($DEBUG) echo "original : '$thestr'<BR>";
  while (!($a=strpos($thestr,'[/url'))===FALSE) { // link found
    $final=$a+strlen('[/url')+1;
    $a=$a-1; 
    $b=$a-1;
    while ($thestr[$b]<>']') $b--;
    $thetext=substr($thestr,$b+1,$a-$b);
    if ($DEBUG) echo "text= '$thetext'<BR>";
    $a=0;
    while (substr($thestr,$a,4)<>'url=') $a++;
    $debut=$a-1;
    $a=$a+4; // or strlen('searchedabove')
    $theurl=substr($thestr,$a,$b-$a);
    if ($DEBUG) echo "url= '$theurl'<BR>";
    // rebuild string
    $thestr=substr($thestr,0,$debut).'<a href="'.$theurl.'">'.$thetext.'</A>'.substr($thestr,$final); // rest
    if ($DEBUG) echo "rebuilt : '".htmlspecialchars($thestr)."'<BR>";
  } // while link found
  if ($DEBUG) echo "final : '$thestr'<BR>";
  return $thestr;
} // AtomSubst String Function

function SubstTag($par, $DEBUG=FALSE) {
  $res=array(); // result
  $n=count($par);
  for ($i=0;$i<$n;$i++) $res[$i]=AtomSubst($par[$i],$DEBUG);
  return $res;
} // SubstTag String[] Function

// test text
$yourmessage=array();
$yourmessage[]="first line";
$yourmessage[]="second line with a [url=http://www.site.com]text[/url] link";
$yourmessage[]="third line";
$yourmessage[]="fourth [url=http://www.site.com]link text2[/url] line with two of them [url=http://www.site.com]link text3[/url]";
$yourmessage[]="fifth line";
// display
echo 'source data :<BR>';
echo '<pre>';
print_r($yourmessage);
echo '</pre>';
// now call the SubstTag function
$result=SubstTag($yourmessage,TRUE); // turn to FALSE to get rid of debug messages
// display
echo 'result data :<BR>';
echo '<pre>';
print_r($result);
echo '</pre>';

Vincent Graux (VGR) for European Experts Exchange and Edaìn Works  back to list of test scripts
Last update 2024-05-13 09:30:58