cimmer
asked on
javascript to php question
below is javascript.... What would be the PHP equivelent??
it converts custom forum tags for display like...
[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>
[linebreak] to <br />
function replacetags(str){ str=str.replace(/\[(\w*)\] /gi,'<$1 />'); str=str.replace(/\[\:(\w*) \]/gi,'</$ 1>');
str=str.replace(/\[(\w*)\: ([^\]]*)\] /gi,'<$1$2 >');
str=str.replace(/\[linebre ak\]/gi,'< br />');
return str;
}
it converts custom forum tags for display like...
[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>
[linebreak] to <br />
function replacetags(str){ str=str.replace(/\[(\w*)\]
str=str.replace(/\[(\w*)\:
str=str.replace(/\[linebre
return str;
}
ASKER
my example has colons...
[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>
how do you add the colon into that??
[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>
how do you add the colon into that??
I don't quite see the point in the colons but here is your text/example...
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
$str = replacetags('[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>');
echo($str);
function replacetags($str){
$str=preg_replace('/\[line break\]/i' ,'<br />',$str);
$str=preg_replace('/\[(\w* )\]/i','<$ 1>',$str);
$str=preg_replace('/\[(\w* )\:([^\]]* )\]/i','<$ 1$2>',$str );
$str=preg_replace('/\[\/(\ w*)\]/i',' </$1>',$st r);
return $str;
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
The only difference is [br] is getting switched to <br> and not <br /> which shouldn't really matter.
Regards,
Kevin
--------------------------
$str = replacetags('[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>');
echo($str);
function replacetags($str){
$str=preg_replace('/\[line
$str=preg_replace('/\[(\w*
$str=preg_replace('/\[(\w*
$str=preg_replace('/\[\/(\
return $str;
}
--------------------------
The only difference is [br] is getting switched to <br> and not <br /> which shouldn't really matter.
Regards,
Kevin
ASKER
your example returns <br> to <b>abc<b> t<a href="anything">abc<a>...
the closing tags arent converting correctly. It still doesnt work.
the closing tags arent converting correctly. It still doesnt work.
my apologies I thought you were trying to convert bbcode which uses [/b] as a closing.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
$str = replacetags('[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>');
echo($str);
function replacetags($str){
$str=preg_replace('/\[line break\]/i' ,'<br />',$str);
$str=preg_replace('/\[(\w* )\]/i','<$ 1>',$str);
$str=preg_replace('/\[(\w* )\:([^\]]* )\]/i','<$ 1$2>',$str );
$str=preg_replace('/\[:(\w *)\]/i','< /$1>',$str );
return $str;
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
basically we just changed the last line before return $str to check for :tag instead of /tag
--------------------------
$str = replacetags('[br] to <br />
[b:]abc[:b] to <b>abc</b>
[a: href="anything"]abc[:a] to <a href="anything">abc</a>');
echo($str);
function replacetags($str){
$str=preg_replace('/\[line
$str=preg_replace('/\[(\w*
$str=preg_replace('/\[(\w*
$str=preg_replace('/\[:(\w
return $str;
}
--------------------------
basically we just changed the last line before return $str to check for :tag instead of /tag
ASKER
closing tags still arent closed...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
awesome
--------------------------
$str = replacetags("[b]this is some bold text[/b]
[linebreak]
[br]
[linebreak]
[i]This is some italic text[/i]");
echo($str);
function replacetags($str){
$str=preg_replace('/\[line
$str=preg_replace('/\[(\w*
$str=preg_replace('/\[\/(\
return $str;
}
--------------------------
Regards,
Kevin