Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing BB tags with HTML tags 1

Status
Not open for further replies.

james0264

Programmer
Mar 1, 2005
52
GB
If I have the following string-
Code:
[someTag=someValue]someText[/someTag]
How do I use str_replace or preg_replace to change the string into something like this?
Code:
<someTag someVariable="someValue">someText</someTag>
Basically, how do I keep the "someValue" bit intact during the function?
 
I don't know where the "someVariable" part of the replace ment comes from, but this outputs something like what you want:

Code:
<?php

$a = '[someTag=someValue]someText[/someTag]';

$b = preg_split ('/[\[\]=]/', $a);

print '<' . $b[1] . ' somevariable="' . $b[2] . '">' . $b[3] . '<' . $b[4] . '>';

?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Yeah, that works brilliantly, many thanks Slripnir214.
 
I just realised, you've used preg_split, which doesn't work with the current script I have. I need to have a script using preg_replace (or str_replace) so that I can change (an example BBCode string):
Code:
[a=[b][URL unfurl="true"]http://someWebsite.com[/URL][/b]][b]someText[/b][/a]
To:
Code:
<a href="[b][URL unfurl="true"]http://someWebsite.com[/URL][/b]">[b]someText[/b]</a>
I know how the _replace functions work, but I am having trouble is passing the variables (highlighted in bold above) from the "search" to "replace".
Many thanks, James.
 
I don't understand. The string you posted:

[a=[URL unfurl="true"]http://someWebsite.com[/URL]]someText[/a]

is output as:

<a somevariable="

The only thing missing is he logic necessary to replace "somevariable" with "href" when dealing with an <A> tag.




Want the best answers? Ask the best questions! TANSTAAFL!
 
True, but if I use the same (exact) code you typed with:
Code:
[i]1)[/i] [someTag]someText[/someTag]
[i]2)[/i] [someTag=someValue]
(note the absense of an attribute in the first example)
It will not produce the following code:
Code:
[i]1)[/i] <someTag>someText</someTag>
[i]2)[/i] <someTag someVariable="someValue" />
But will product something like this:
Code:
[i]1)[/i] <someTag someVariable="someText">/someTag<>
[i]2)[/i] <someTag someVariable="someValue"><>
I'm sorry for the inconvenience, but I hope I have finnally clarified my problem. Furthermore, someVariable is an additional attribute found in HTML that will not be nescessary in the BBcode, so ignore it for now. Many thanks again, James.
 
Hmph. I can only answer the question you actually ask, not the question you keep privately in you head.


It's just a matter of splitting the string as before, then using logic to output the correct string:

Code:
<?php

$a = array ('[someTag=someValue]', '[someTag]someText[/someTag]', '[someTag=someValue]someText[/someTag]');

foreach ($a as $string)
{
	
	print $string . '  =>  ';
	$string_parts = preg_split ('/[\[\]=]/', $string);
	
	switch (count($string_parts))
	{
		case 4:
			print '<' . $string_parts[1] . ' somevariable="' . $string_parts[2] . '" />';
			print "\r\n";
			break;
		case 5:
			print '<' . $string_parts[1] . '>' . $string_parts[2] . '<' . $string_parts[3] . '>';
			print "\r\n";
			break;
		case 6:
			print '<' . $string_parts[1] . ' somevariable="' . $string_parts[2] . '">' . $string_parts[3] . '<' . $string_parts[4] . '>';
	}
}

?>

produces:

[tt][someTag=someValue] => <someTag somevariable="someValue" />
[someTag]someText[/someTag] => <someTag>someText</someTag>
[someTag=someValue]someText[/someTag] => <someTag somevariable="someValue">someText</someTag>[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
This is perfect. I can't stress "thanks" enough, have a star as well! Many thanks again, James.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top