Jump to content

Read String


MoFish

Recommended Posts

Hi,

I have the following code inside my image.php file which I can read okay:

<mofish id="image" type="image" label="Please select an image" required="true" title="true" />

However if i put my tag amoungst other HTML it doesnt seem to find it. Can anybody shed any light on this? I've tried changing the quotes around etc with no success.

<img src="<mofish id="image" type="image" label="Please select an image" required="true" title="true" />" />

Ideally i need to be able to read the mofish tag regardless of what is around it.

Regards,

MoFish
 

$html = file_get_contents("placeholder_templates/image.php", true);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName("mofish");

foreach ($tags as $tag) {    
    
    echo "<pre>";
    print_r ($tag);
    echo "</pre>";
}
Link to comment
Share on other sites

Your syntax has nothing whatsoever to do with HTML or XML, so DOMDocument has no chance of parsing it. Even if you fix the quotes, you'll only get a single img element with a very strange source URL (as opposed to an img element and a mofish element).

 

What are you trying to do? If you want to insert data into an HTML document, you need a template engine, not an HTML parser.

Link to comment
Share on other sites

Hi Jacques1,

What i am trying to do is read a HTML file (image.php) which could in theory contain multiple <mofish> tags.

I want to loop around each tag and find out the 'type' attribute associaited with each tag.

Once I have found the type - I can then build up a simple form using the attributes. Please see the below as an example:

<img src="<mofish id="image" type="image" label="Please select an image" required="true" title="true" />"  alt="<mofish id="text" type="text" label="Image alternative text" required="true" title="true" />" />

This would generate a form like the following: (i'm ignoring required and title for now).

<label>Please select an image</label>
<input type="file" name="" />

<label>Image alternative text</label>
<input type="text" name="" />

The example I put together didnt work, but you might get the general idea from the below:

$html = file_get_contents("placeholder_templates/image.php", true);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName("mofish");

// find out which tag it is
foreach ($tags as $tag) {	
	switch ($tag->getAttribute('type')){
		case "text":
			// display text
			echo "<div class='row'>";
				echo "<label for=''>".$tag->getAttribute('label')."</label>";
				echo "<input type='text' name='' value='' />";
			echo "</div> <br/>";		
			
			break;
		case "image":
			// display file upload
			echo "<div class='row'>";
				echo "<label for=''>".$tag->getAttribute('label')."</label>";
				echo "<input type='file' name='' value='' />";
			echo "</div> <br/>";
			break;
		default:
			echo "could not find";
	}
}	

MoFish

Link to comment
Share on other sites

I understand that you want to use your mofish elements as placeholders, but putting a mofish tag into an img tag is neither valid, nor does it make any sense.

 

You can have this:

<img src="..." alt="...">
<mofish ...>
<mofish ...>

That's an img element followed by two mofish elements.

 

But you cannot have this:

<img <mofish> <mofish>>

That's just nonsense.

 

And again, I doubt that you're on the right track. Generating dynamic HTML with custom tags is what template engines are for.

 

Theoretically, it's possible to implement templates with pure XML using XSLT, but this will be very complicated and may require you to rewrite large parts of your HTML documents. Because then you'll need XHTML as opposed to good old HTML.

Link to comment
Share on other sites

Hi Jacques1,

 

The reason for the <mofish> placeholders being in amoungst HTML, is that I thought my administration panel could generate form as explained above, and from the front end of the website the <mofish> tags could be fully replaced with the database values inserted in the form. For example the following:

<img src="<mofish id="image" type="image" label="Please select an image" required="true" title="true" />"  alt="<mofish id="text" type="text" label="Image alternative text" required="true" title="true" />" />

Administration:

 

This would look at the following string, and generate form fields as explained above - ignoring the syntax around the <mofish> tags and generate a form based on the attribute values.

 

Front End

 

This would look at the above string and replace the <mofish> tags with the database values assigned from the administration form above, giving an output of something like:

<img src="image1.jpg"  alt="Welcome to my page" />

That was the idea in my head, but I understand its a little strange!

Link to comment
Share on other sites

It's not just a little strange. It makes no sense at all.

 

Even if you could implement this with tons of code, nobody except you would understand it. And that's the last thing you want when you implement a GUI.

 

So I suggest you get back to a more sane approach. You want a form? Write a form. You want to insert the data into an image element? Insert the data into an image tag. No magic.

Link to comment
Share on other sites

Don't let Jacques bother you. He's like that to everyone.

 

One way or another, what you're trying to do can be possible. How much of this code is yours? Or put another way, how familiar are you with the PHP involved in this process and are you able to edit it?

Link to comment
Share on other sites

Many things are “possible”. For example, it's “possible” to implement a complete MVC framework with Brainfuck:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>>++.+++.+++++++++.+.+.>++.<-----.---------.>.<-----.+++++++++++++++.++.---------.+++.>.<------.+++++++++..---.+++++++.>+.>.+++++++++>+++++++>++++++++++>+++>+<<<<->>++++++++++++++++>++<--------------><-----+++++++++++++++++---------+++><------+++++++++---+++++++>+>+++++++>+++++++>+++++++++>++>+<<<<->>+++++++++++++++>+<-------------><----+++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++++>+++++++>+++++++++>++>+<<<<->>+++++++++++++++>+<-------------><----+++++++++++++++---------++><-----+++++++++--++++++++>+++++++>+++++++>+++++++++>++>+<<<<->>+++++++++++++++>+<-------------><----+++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--+++++++>++++++>++++++>+++++++++>++>+<<<<->>+++++++++++++++>+<-------------><----+++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--++++++>++++++++++++>++++++++>+++<<<<->>+++++++++++++++>+<-------------><----+++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++>++++++++++>+++++++>+++<<<<>>++++++++++++++>+<-------------><----+++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-++++++>++++++++++++++++>++<<<<>>++++++++++++>+<------------><----+++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-++++++>++++++++++++++>++<<<>>+++++++++++>+<----------><----++++++++++++++---------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++++++++++++>++<<>>++++++++++>+<---------><---+++++++++++++--------++><-----+++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++>++<<->+++++++>----------+++++++-----+---+++-+++++>+++++++++++>++<<>+++++++++>+<--------><---+++++++++++--------+><-----++++++++--++++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++<->+++++++>---------++++++--------+++-++++>++++++++++>++<>++++++++>+<-------><---++++++++++-------+><----+++++++--+++++++>++++++++++++>++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++<->++++++>--------++++++-------++-++++>+++++++++>+<>+++++++>+<------><---+++++++++------+><----++++++--++++++>+++++++++++++++++++>++>+<<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++>+++++>--------+++++------++-++++++++++++>+<>++++++>+<-----><---++++++++-----+><----+++++--+++++>+++++++++++++++++>++>+<<->>+++++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++>--------++++------+++++++++++++>+<>+++++>+<----><---+++++++-----><----++++--+++++>+++++++++++++++>+>+<<->>+++++++++++>+<------------><---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++>-------++++-----++++++++++++>+>+++++>+<---><---++++++----><----+++--+++++>+++++++++++++>+>+<->>++++++++++>+<-----------<---++++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++------++++-----++++++++++>+>++++>+<---><--++++++---><----++--+++++>+++++++++++>+>+<->+++++++++>+<----------<---++++++++++++--------++<-----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++---++++-----+++++++++>+++++>+<--><--+++++---><----+--+++++>+++++++++>+>+<->++++++++>+---------<---+++++++++++-------++<----++++++++--++++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-+++-----++++++++>++++++<--><--++++---><---+--++++>++++++++>+>+<->+++++++>+--------<--++++++++++-------+<----+++++++--+++++++++++++++>+++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++----++++++++++++++<--<--++++--><---+--+++>+++++++>+>+<->++++++>+-------<--+++++++++------+<---+++++++-++++++++++++++>++++++>++>+<<<->++++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++---+++++++++++++<--<--+++--><-----+++>++++++>+>+<>++++++>+------<--++++++++-----+<---++++++-++++++++++++>++++++++>+<<<->+++++++++++>+<-----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-++++++++++++<-<--+++--<-----+++++++++>++<>+++++++------<-++++++++----+<---+++++-+++++++++++>+++++++>+<<->++++++++++>+<----------<---+++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++++++++++<-<-+++--<----++++++++>++<>++++++-------++++++++---+<---++++-++++++++++>++++++>+<<-+++++++++>+<---------<---++++++++++++-------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++++++++<-<-+++-<----++++++++>++<++++++------+++++++---+<---+++-++++++++++>+++++>+<<-++++++++>+<--------<--+++++++++++------++<----+++++++--+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++++<-<-+++-<---+++++++>++<+++++------++++++---+<--+++-+++++++++>++++>+<<-+++++++>+<-------<--++++++++++-----++<---+++++++-+++++++++++++>+++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++-<-+++-<---++++++>++<++++-----++++++---<--+++-++++++++>+++>+<<-++++++>+<------<--+++++++++----++<---++++++-++++++++++++>++++++>+++<<<->+++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++-<+++-<---+++++>++<++++----+++++---<--+++++++++++>++>+<<-+++++>+<-----<--++++++++----+<---+++++-+++++++++++>+++++>+++<<<->++++++++++><----------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++<++-<---+++++>+<++++----++++---<-++++++++++>++>+<<-++++>+<----<--+++++++----+<--+++++-+++++++++++++++>+++<<->+++++++++><---------<---++++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++-<--+++++>+<+++----++++--<-+++++++++>++>+<-++++>+<---<--++++++----+<-+++++-+++++++++++++>+++<<-+++++++++<--------<---+++++++++++------++<---++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++<--++++>+<+++---++++--<-++++++++>+>+<-+++++<---<--+++++----+-+++++-+++++++++++>+++<<-++++++++<-------<---+++++++++------++<--++++++--++++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-+++>+<+++---+++--<-+++++++>+>+<-++++<---<-+++++---+-+++++-+++++++++++++<<-+++++++<------<---++++++++-----++<--+++++--+++++++++>++++++>+++<<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++><+++---+++---+++++++>>+<-++++<--<-+++++--+-+++++++++++++++++<<-++++++<-----<---+++++++-----++--+++++--++++++++>+++++>+++<->++++++++++><---------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++><++---+++---++++++>>+<-+++<--<-++++--+-+++++++++++++++<<-+++++<--------++++++-----++--++++--+++++++>++++>+++<->+++++++++><--------<---++++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++--+++---+++++>>+<-+++<-<-++++---++++++++++++++<<-++++<-------++++++----++--+++--++++++>++++>++<->++++++++><-------<---+++++++++------++---++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++--++---+++++>>+-+++<-<-+++---+++++++++++++<-++++<------+++++----++--++--++++++++++>++<-++++++++>-------<--++++++++------++--++++++-+++++++>+++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++--+++++>>+-++<-<-+++--++++++++++++<-++++------++++----++--+--+++++++++>++<-+++++++>------<--+++++++-----++--+++++-++++++++++++>+++<<->+++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++--++++>>+-++<-<+++--+++++++++++<-+++------+++----++-+--++++++++>++<-++++++>-----<--++++++-----++-+++++-++++++++++++++<<->++++++++>------------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++>>+-++<-<+++-++++++++++<-+++-----+++---++-+--+++++++>++<-+++++>----<--+++++-----++-++++-+++++++++++++<->+++++++>-----------+++++++++------+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++>>-++<-<+++-+++++++++<-++-----++---++-+--++++++>++-+++++>---<--+++++----++-+++-++++++++++++<->++++++>----------++++++++-----+---+++++-+++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++>-+<-<+++-++++++++<-++----++---+++--++++++++-+++++>--<--++++----++-++-+++++++++++<->+++++>---------+++++++-----+--+++++-++++++++>+++<<->++++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++<-+++-++++++++-++----++--+++--+++++++-++++>--<--+++----++-+-++++++++++<->++++>--------+++++++----+--++++-+++++++++++<<->+++++++>-----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-++-++++++++-++---++--++--+++++++++++>--<-+++----+++-+++++++++<->++++--------++++++------++++-++++++++++<->+++++++----------++++++++-----+---++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++-+++++++-++---+--++--++++++++++>-<-+++---+++-++++++++<->+++--------+++++-----++++-+++++++++<->++++++---------+++++++-----+--++++-++++++>++<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++++++-++---+--+--+++++++++>-<-++---+++-+++++++<->++--------++++-----+++-++++++++<->+++++--------+++++++----+--+++-++++++>+<<->+++++++>----------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++-++---+--+--++++++++><-++---++-+++++++<>++-------++++----+++-+++++++<->++++--------++++++------+++-+++++>+<<->++++++>---------+++++++-----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++-+---+--+--+++++++><-++--++-++++++<>++------++++---+++-++++++<->++++-------+++++------++-+++++><<->+++++>--------+++++++----+---+++-++++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++--+--+--++++++><-++--++++++++<>+------+++---+++-+++++<->+++-------++++------+-+++++><<>+++++>-------++++++----+--+++-+++++<->+++++++>---------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-+-+--++++++><-+--+++++++<>+-----+++---++++++++<->++-------+++------++++++><<>++++>------++++++---+--+++-++++<->++++++>--------++++++--------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-+-++++++><-+--++++++<>+----+++--++++++++<>++------+++------+++++><<>+++>-----++++++-----+++-+++<->++++++--------+++++-------+++-+++<->++++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-+++++><-+--+++++<>+----++--+++++++<>++-----+++-----+++++><<+++>-----+++++----+++-+++<-++++++-------+++++------++++++<->+++++>--------++++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++++><-+--+++++<+----++--++++++<>+-----+++----+++++><+++>----+++++----++-+++<-+++++------+++++-----++++++<>+++++>-------+++++-------++-+++>+++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-++++++><+--+++++<+---++--+++++<>+-----++----++++><+++>---+++++---++-+++<-++++------++++-----+++++<>++++>------+++++------++-+++>++++>--------+++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++<+-+++++<+---++-+++++<>+----++---++++><+++---+++++--++-+++<-+++------+++-----++++<>++++------++++------+++++>++++>-------++++------++++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++-++++<+---++-++++<>+----+---++++>+++---++++--++-+++-+++-----+++-----+++<>++++-----++++-----+++++++++>------++++-----+++++++>--------++++------+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++++---++-++++<+----+---+++>+++--++++--++-++-+++----+++-----++<>++++----++++----++++++++>------+++-----++++++>-------++++-----+++++>-------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++++--++-++++<+---+---+++>++--++++-++-++-+++---+++----++<>++++---++++---++++++++------+++----+++++>-------+++-----++++>------++++-----+++------++++----+++---++++---+++-+++---+++++--+++-+++++--+-++++<+---+--+++>++--+++-++-+++++---+++---++<>+++---++++--++++++++-----+++---+++++>------+++----++++>-----++++----+++-----++++----+++---++++---+++-+++---+++++--+++-++++++-+++<+---+--++>++--+++-+-+++++--+++---++<+++---+++--+++++++-----+++--+++++>-----+++---++++>----++++----++-----+++----+++--++++---+++-+++---+++++--+++-+++++++++---+--++>++-+++-+-++++--+++--++<+++---++--++++++-----+++-+++++>----+++---+++>----+++----++----+++----++--++++--+++-+++--+++++--+++-+++++++--+--++>++-++-+-++++--++--++<++---++--+++++-----

(taken from Brainfuck on Rails)

 

Implementing the OP's orginal idea might be a similarly interesting intellectual challenge. I'll give you bonus points if you do it with Brainfuck.

 

However, I believe we should concentrate more on simple, pratical solutions and less on nerd challenges. When generating a friggin' HTML form requires the OP to have advanced PHP knowledge and make fundamental changes to the underlying process, you're doing it wrong.

 

I'm sure this can all be said in a more friendly way, but the point is really: Keep simple things simple. I can understand that the OP wants to save a few keystrokes, which is why I suggested a template engine with macro support. But a magical context-dependend, shape-shifting HTML element is just nonsense. Sorry, guys.

Link to comment
Share on other sites

Hi,

 

Sorry, I don't mean to make things confusing. I had an overall picture of how I wanted my project to work, and feel it could work if i could break it down and figure out the small components. For the moment i'm focusing on building a form from the contents of the file (image.php). I have played around with things since the orignal post and have got a little further, however am again running into problems. My code now finds the <mofish> tags in the file amoungst other HTML which I then read using the DOMDocument. In my final $string, when I echo it, it writes back the fields as I would expect, however when I do a return on the $string it only returns one field. As Jacques said, maybe i'm going down the wrong road completely - but feel i'm close in getting the form fields displaying which is all I need for the time being.

 

@requinix - the code is mine - and I can edit it anyway fit get the form fields to display.

	public function generate_form($filename='image.php')
	{
		$html = file_get_contents("./placeholder_templates/".$filename, true);
		preg_match_all('/<mofish[^>]+>/i',$html, $results); 
		
		foreach($results['0'] as $result){
			//echo htmlentities($result) . "<br/>";
			
			$doc = new DOMDocument();
			@$doc->loadHTML($result);
			$tags = $doc->getElementsByTagName("mofish");
			$string = "";
			
			foreach ($tags as $tag) {

				switch ($tag->getAttribute('type')){
					case "text":
						$string .= "<div class='form-group'>
										<label for=''>".$tag->getAttribute('label')."</label>
										<input class='form-control' type='text' name='' value='' />
									</div>";
						break;
					case "image":
						$string .= "<div class='form-group'>
										<label for=''>".$tag->getAttribute('label')."</label>
										<input class='form-control' type='file' name='' value='' />
									</div>";
						break;
					default:
						echo "could not find";
				}
				//echo $string;
			}
			
			//echo  $string;
			return $string;
		}
		
	}
Edited by MoFish
Link to comment
Share on other sites

With Twig:

 

main template

{% import "form.twig" as form %}

<form method="post" enctype="multipart/form-data">
    {{ form.mofish_image("foo_image", "Please select an image") }}
    {{ form.mofish_image_alt_text("foo_image_alt_text", "Image alternative text") }}
    <input type="submit">
</form>

macros (custom form elements)

{% macro mofish_image(field_name, label) %}
    <div class="form-group">
        <label>{{ label }}</label>
        <input type="file" name="{{ field_name }}">
    </div>
{% endmacro %}

{% macro mofish_image_alt_text(field_name, label) %}
    <div class="form-group">
        <label>{{ label }}</label>
        <input type="text" name="{{ field_name }}">
    </div>
{% endmacro %}

The input gets auto-escaped, so you don't have to worry about XSS attacks.

 

I'm not necessarily saying this is exactly how I'd do it. But it demonstrates that you can save a lot of typing and headaches if you use a professional template engine instead of trying to build your own with a bunch of DOMDocument hacks.

Link to comment
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.