Proper replacements for symbols and such..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Proper replacements for symbols and such..

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Proper replacements for symbols and such..

Post by social_experiment »

“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Proper replacements for symbols and such..

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Proper replacements for symbols and such..

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Proper replacements for symbols and such..

Post 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?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Proper replacements for symbols and such..

Post 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.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Proper replacements for symbols and such..

Post by Pazuzu156 »

Thanks, I'll play around with it, see what I can come up with.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply