Jump to content

Recommended Posts

hey peeps

 

i have made a simple CMS and im trying to add a bit more functionality it. i'm trying to do this by adding a simple text formatter. as it stands im having to create my own bbcode functions because my host will not add the PEAR BBCodeParser.

 

ive got the whole insert the tags into the textarea sorted.

 

the only problem is the parser.

 

this is what i'm working on at the moment, but it really doesn't output what i expect:

<?php

function bbCode($string) {

//formatting
$string = ereg_replace("[b]", "<strong>", $string);
$string = ereg_replace("[/b]", "</strong>", $string);
$string = ereg_replace("[u]", "<u>", $string);
$string = ereg_replace("[/u]", "</u>", $string);
$string = ereg_replace("[i]", "<i>", $string);
$string = ereg_replace("[/i]", "</i>", $string);

//links


//images
$string = ereg_replace("[img]", "<img src=\"", $string);
$string = ereg_replace("[/img]", "\" alt=\"Image\" />", $string);

return $string;

}

?>

 

when testing it it outputs this:

[<stron<" alt="Image" />" alt="Image" />" alt="Image" /> src=">]

while it should just output <strong>

 

This is the test in running:

<?php

$string = "[b]";

$string = bbCode($string);

$string = htmlentities($string);

echo $string;

?>

 

any ideas, suggestions?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/119200-text-replacement/
Share on other sites

Square brackets are metacharacters that create character classes, so they need to be escaped to match literals: \[b\]. Additionally, do not use regex when working with static strings--use str_replace.

 

Signed,

 

A peep.

 

LOL I just laughed pretty hard at that Peeps thing.

Link to comment
https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-613924
Share on other sites

Square brackets are metacharacters that create character classes, so they need to be escaped to match literals: \[b\]. Additionally, do not use regex when working with static strings--use str_replace.

 

Signed,

 

A peep.

 

Just for educational purposes, the reason for doing this is that regex is very slow compared to PHP's build in string functions... usually around 30-40x slower for the most basic comparisons.

Link to comment
https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-613936
Share on other sites

thanks for the info.

 

however i'm still having a bit of trouble with creating the function.

this is what i have:

<?php

function bbCode($string) {

$bbcodes = array(
	'\[b\]',
	'\[/b\]',
	'\[i\]',
	'\[/i\]',
	'\[u\]',
	'\[/u\]'
)

$htmlcodes = array(
	'<strong>',
	'</strong>',
	'<em>',
	'</em>',
	'<u>',
	'</u>'
)

$string = str_replace($bbcodes, $htmlcodes, $string);

return $string;

}
?>

i'm still running the same text and i am having no luck with it.

Am i doing something wrong?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-614317
Share on other sites

ok i removed the escaping and added the semi colons

 

and the output is '3'

 

here is my new source:

function bbCode($string) {

$bbcodes = array(
	'[b]',
	'[/b]',
	'[i]',
	'[/i]'
);

$htmlcodes = array(
	'<strong>',
	'</strong>',
	'<em>',
	'</em>'
);

$string = str_replace($bbcodes, $htmlcodes, $string);

return $string;

}

 

testing script:

<?php

include 'functions.php';

$string = "[b]bllaaaahhhhhh[/b]";

$string = bbCode($string);

$string = htmlentities($string);

echo $string;

?>

help?

im an unhappy bunny :(

Link to comment
https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-614955
Share on other sites

hmm it works now,

 

one question, how would i parse links?

links would ideally look like this in BBCode:

Link to blah

 

is there a way of getting the contents of the link and the text for the link then wrapping them appropriately?

 

i was going to do it like so:

add '[url=' to my array of bbcodes then replace it with '<a href="' then do a similar thing for the end..

 

:S

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-615002
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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