View Full Version : Vb Code.
MrSheen
05-23-2007, 08:02 AM
I see in forums, im finding the replace [B] etc with <b>.
Now, i could use things such as:
str_replace ( '[B]', '<b>', $string );
but the i would have to type out all the possiblilties.
for example:
str_replace ( '[color=red']
str_replace ( '[COLOR=RED']
str_replace ( '[CoLoR=Red']
and so on, how would i easily make this script? Any ideas?
djr33
05-23-2007, 09:32 AM
I wouldn't think anyone would likely use CoLoR.... seems silly.
Accounting for caps and lowercase makes some sense (overall, for the whole word), but just using one should be fine. Certainly using both would be fine, I think. I don't think you'd need to account for anything major.
You could do it by using--
<?php
$string = //define this;
$srch = '[color=red]';
$rep = '<span color="red">';
$stlow = strtolower($string);
for ($pos = strpos($srch,$stlow,$pos+5)) {
$s1 = substr($string,0,$pos);
$s2 = $rep;
$s3 = substr($string,$pos+strlen($srch));
$string = $s1.$s2.$s3;
$stlow = strtolower($string);
}
echo $string; //now it's done
?>
That's untested, but I think is along the right lines.
Using regular expressions ("regex") might be very beneficial too if you want to look into something that complex. They basically allow you to search for patterns in the text.
tech_support
05-23-2007, 09:33 AM
Hmm...
How about "colour"? (British spelling)
djr33
05-23-2007, 09:37 AM
I agree that's prettier, but in terms of programming, everything, even programs written in/for foreign languages use American English as the code, since that is primarily where it is developed, so that everyone uses it for consistancy.
That's like using Italian (or Greek) instead of Latin to name an animal species.
Now, seriously, if you needed that as well, I'd say just repeat the above functions.
Really, you could put the entire thing into a for each loop and use arrays to store pairs of search/replace strings to edit the text of the string. This could include multiple spellings.
Including a multiple spelling within a single search term would be tough, since you'd need to note position and what it is, then tell the search function when to do which, etc. Just easier to search twice if the spelling actually differs.
tech_support
05-23-2007, 09:38 AM
My English teacher would kill me if I wrote "color". :p
And, to make this more valid:
<?php
$string = //define this;
$srch = '[color=red]';
$rep = '<span style="color:#FF0000;">';
$stlow = strtolower($string);
for ($pos = strpos($srch,$stlow,$pos+5)) {
$s1 = substr($string,0,$pos);
$s2 = $rep;
$s3 = substr($string,$pos+strlen($srch));
$string = $s1.$s2.$s3;
$stlow = strtolower($string);
}
echo $string; //now it's done
?>
MrSheen
05-23-2007, 10:37 AM
Hey guys, seems good.
I think i like the idea of using the "strtolower) function.
This will mean i will only have to do it once, in lower case right?
i will just have to include
strtolower ( $string );
before i do anything else.
Thanks for your help :)
djr33
05-23-2007, 10:49 AM
Oh, note-- strtolower, not str2lower. Several functions in PHP use '2' in them, but I forgot this isn't one of them. //Edited in previous posts.
Using strtolower() will give you a copy of the string with all lowercase letters. This will change EVERYTHING, including the posts.
This is why I had to do a sort of mirror operation, checking the lowercase version for position, then mapping that onto the real string to get a final result with the correct replacements and correct capitalization. It's complex, but that type of approach will be needed if you must allow for oddly capitalized tags. I say you don't really need them, but can't hurt, I guess.
MrSheen
05-23-2007, 10:53 AM
Oh, note-- strtolower, not str2lower. Several functions in PHP use '2' in them, but I forgot this isn't one of them. //Edited in previous posts.
Using strtolower() will give you a copy of the string with all lowercase letters. This will change EVERYTHING, including the posts.
This is why I had to do a sort of mirror operation, checking the lowercase version for position, then mapping that onto the real string to get a final result with the correct replacements and correct capitalization. It's complex, but that type of approach will be needed if you must allow for oddly capitalized tags. I say you don't really need them, but can't hurt, I guess.
Its just new people will end up doing stuff like that, im making it more user freindly if you get me :)
djr33
05-23-2007, 10:54 AM
If you're up for dealing with more complex code, that works.
but even for new people, if it doesn't work, they'll try to figure out what's wrong, then just fix it. Everyone can learn.
...or not, in which case you'd want this ;)
Good luck.
MrSheen
05-23-2007, 10:55 AM
Hmm. So you think its best just to use CAPITAL letters for the code? Then when its wrong, the will figure out and edit it.
Hmm, could be a possibilty. Would be a lot easier to code :P
djr33
05-23-2007, 10:58 AM
I'd say lowercase.
Rarely would I bother, just for fun, capitalizing letters for code.
[b].... not [B].
However, as I said it would not be tough to have two versions, one for lower and one for upper case.
This wouldn't account for any random mix, but I see no reason there would ever be a random mix of letters and certainly that the user would figure it out after a while.
MrSheen
05-23-2007, 10:59 AM
Alright, thanks for you help :) I shall link you to the finished product once i have done :) Thanks for your help :)
I agree that's prettier, but in terms of programming, everything, even programs written in/for foreign languages use American English as the code, since that is primarily where it is developed, so that everyone uses it for consistancy.Not wxWidgets. They have a few "color" aliases in places, but it's mostly just "colour."
And, to make this more valid:Or, in fact, work at all :p All of you so far have forgotten to close the tags... this is a lot trickier. You don't want the close tag for [b], which would become <span style="font-weight: bold;">, for example, to work to close [color].
The OP was talking about vBulletin code, as seen on this forum, not the language Visual Basic.
It's not a case for regex so much as a recursive parsing function, I think, although admittedly probably in conjunction with regex. I guess it might be possible with pure regex, but the recursive function would be much easier.
This is the basic regex you want to find: \[(.*?)\]
This is what you want to replace it with: <$1>Don't do this. It'll open you up to all sorts of irritating spam messages using Javascript and cross-site scripting attacks.
djr33
05-24-2007, 02:33 AM
Well, if you did that and had some sort of 'nice' list with the *, then that might work. I'm not sure if tha'ts possible with regex or not.
Is there a way in regex to use an array or other set of values as a chunk?
\[(.$array?)\]
Where $array holds the value of all possible tags
And, furthermore, you'd need then a counterarray to list the start tags (and close tags, in a third, perhaps).
Trinithis
05-24-2007, 04:58 AM
You could try something like this:
/\[((tag1)|(tag2)|(tag3))\]/
Which makes me want to code a method for JS that would allow the concatenation of regexes. (Because it would be cool to define the tagname pattern seperately from the bracket pattern.)
Is there a way in regex to use an array or other set of values as a chunk?
\[(.$array?)\]
Where $array holds the value of all possible tagsNo, but you can just do:
'(' . implode('|', $array) . ')'
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.