Page 1 of 1
Proper replacements for symbols and such..
Posted: Fri Aug 03, 2012 12:53 am
by Pazuzu156
I had a crazy question. I'm creating a script language for a page content form for admin dashboards. Yeah, I can use BBCode, but I'm doing it to learn it for myself.
So, say I have something like:
[syntax][ks:img src="
http://www.example.com/images/example_image.jpg" desc="Image Description"][/syntax]
How would I turn it into:
Code: Select all
<img src="http://example.com/images/example_image.jpg" alt="Image Description">
*EDIT*
====================
It's for multi-line editing. So they have that bit of code in there and a bunch of other text. Only that piece will be transformed. Needing help, kinda stumped.
Re: Proper replacements for symbols and such..
Posted: Fri Aug 03, 2012 2:37 am
by social_experiment
Re: Proper replacements for symbols and such..
Posted: Fri Aug 03, 2012 3:05 am
by Pazuzu156
Yeah, I know about that function. I'm trying to do this in an object oriented way so it will also act as a code debugger and show mistakes in the users code. Like img must have a src. I want that to be checked and error thrown if not there. And I think I got the str_replace where it loops with a for loop from the keyword array.
Whereas my array looks kinda like:
Code: Select all
<?php
$lang = array(
'img' => array(
0 => 'src',
1 => 'desc'
)
);
?>
Something similar to that.
Re: Proper replacements for symbols and such..
Posted: Fri Aug 03, 2012 5:16 pm
by pickle
preg_match() might be the best bet. You can make a different pattern to check for each required attribute, while at the same time extracting the necessary values.
Re: Proper replacements for symbols and such..
Posted: Fri Aug 03, 2012 6:46 pm
by Pazuzu156
Actually not a bad idea. Thanks. How would I go about doing that where I don't have to use that for every element. Like, use it once to check for an element with one attribute yet also allowing multiple ones?
Re: Proper replacements for symbols and such..
Posted: Sat Aug 04, 2012 5:41 pm
by tr0gd0rr
Instead of constructing regexes for each attribute, try using
preg_match_all to find all the strings with bracket something bracket. Then run preg_match_all on each something to find the string equals quotation mark something question mark. Hard to explain without code, so here is an example:
Code: Select all
$elements = array();
preg_match_all('/\[(.+?)\]/', $input, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
preg_match_all('/(\S+?)="(.+?)"/', $match[1], $attrValues, PREG_SET_ORDER);
$attrs = array();
foreach ($attrValues as $m) {
$attrs[$m[1]] = $m[2];
}
$elements[] = $attrs;
}
Code: Select all
Array
(
[0] => Array
(
[src] => http://www.example.com/images/example_image.jpg
[desc] => Image Description
)
)
You'll probably want something more robust that allows newlines, apostrophes instead of quotation marks, allowing for no matches, extracting the ks:img part, etc.
Re: Proper replacements for symbols and such..
Posted: Sun Aug 05, 2012 9:37 pm
by Pazuzu156
Thanks, I'll play around with it, see what I can come up with.