9 Replies - 6571 Views - Last Post: 27 July 2011 - 08:01 AM Rate Topic: -----

#1 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Anyone know how to work with a html file to generate?

Posted 09 June 2011 - 12:20 AM

Hi, I got one question... how does one go about opening a html file and making changes to it?

For instance I am trying to make a editor for my clients that they can delete and add elements to their webpage.
How using php can I open up their webpage html file and find what line that code is one and where exactly that code starts and ends.

For example lets say my client selects the editor to delete youtube video 6. The id of the element is yt6.

how would I know on what line yt6 is on in the html file and where it starts and ends so I can just delete that code and not anything else on that line. Like for instance if on line 6 there was yt5, yt6, yt7. How can I just delete the yt6 code and not delete the yt7 and yt5 code.

I looked around for tutorials but found the basics of manipulating text in a text file. Where you would just delete that one whole line.

I would like to learn how you can search in a html for code this also goes for javascript and delete the code. Then check if that line is empty and if so then delete it.

Also, notice that with php you can't really just add new code to the last line since html needs to be inside the html and body tags. So is there a way to detect where exactly these ending tags are located but not limited to that but also to divs.... cuz the client might made a new div to add a table looking thing on their webpage.

To sum up what I am asking... how do you manipulate the html files so you can edit them. You can delete certain parts of the code or add new code to certain parts without deleting any code that isn't supposed to be deleted or to put the new code in the wrong area.

For example lets say a client was a message post on their webpage... so they will click a button that says create a window... they won't know what actually goes on in the background but we programmers would know... we would be generating a blank div. Then they hit a drop down that has a list of functions and can config that function. They end up running that. Which would be php code added in a way that would generate that users messages give to them in this div.

Hope this made any sense.. My goal is to work with html,css,javascript,php files. that based on user input from the clients they can created their own webpage just like programmers without the need to code it themselves. To be able to do this goal. I need to know how using php to open up files and editing them without destorying any code or break any parts of the code.

Is This A Good Question/Topic? 0
  • +

Replies To: Anyone know how to work with a html file to generate?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Anyone know how to work with a html file to generate?

Posted 09 June 2011 - 04:35 AM

PHP DOM
Was This Post Helpful? 3
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2992
  • Posts: 10,337
  • Joined: 08-August 08

Re: Anyone know how to work with a html file to generate?

Posted 09 June 2011 - 05:59 AM

You're talking about separating your code, and that's always a good idea.
Was This Post Helpful? 1
  • +
  • -

#4 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Anyone know how to work with a html file to generate?

Posted 09 June 2011 - 08:15 AM

If you want true code separation I would advise that you start googling the life out of MVC architecture. There are plenty of frameworks out there that can help you do that - my recommendation would be to write your own for the simple reason that you will get a great understanding of how MVC architecture works. If you want a fast track, I'd recommend looking up Smarty - PHPs official 'unofficial' template engine. If you have a good understanding of OOP and modifying the .htaccess file you should take to it like a duck to water.

To fit an answer to your question more specifically you can write PHP to modify HTML directly - you could approach this in several different ways, for example, you could build a library of PCREs and preg_match_all the life out of an HTML file everytime you want to make an update, but I would seriously recommend against that (problems with performance, stability, scalability, security etc). That would only suit a one-time-per-scrape screen-scraping application or something of that nature.

The last point I would like to make (I feel like I'm off exploring miles away from your question but bare with me), is that if you want to have / give direct editing control of block objects in your HTML page and you can afford to create an HTML5 only app, then you might want to look at the "contenteditable" attribute - google it and have some fun with it. I've built a beta CMS based on the contenteditable attribute which I'm hoping to roll into production in a years time (simply for compatibility coverage).

Good luck!
Was This Post Helpful? 0
  • +
  • -

#5 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: Anyone know how to work with a html file to generate?

Posted 27 June 2011 - 01:42 PM

View PostJackOfAllTrades, on 09 June 2011 - 05:35 AM, said:



Thanks but would this allow me to search for specific code and delete it or edit it.

For instance lets say I have this:

<a href="google.com" style="font-color:grey;">Click Here for google access </a>


lets say I have this inside a html type file or inside a php file that echos the html code.

what if I want to change or edit the text Click Here for google access and instead put a php variable of the user's name to display it on the link. So it be like username access to google sources.

the username would the the persons registered name.

So I want to manipulate everything inbetween the tags and even the tags themselves. In our example like if I wanted to change the url address I should be able to use php to make those changes.
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Anyone know how to work with a html file to generate?

Posted 27 June 2011 - 03:11 PM

The DOMDocument object can manipulate all aspects of valid HTML. It's got powerful methods like getElementsByTagName which will return a list of the specified elements. If you're familiar with Javascript's DOM Object then this won't be hard to grasp. Quick demonstration below...

Example
<?php
//click any link and enjoy
ini_set('display_errors', false); //sometimes we get invalid HTML but you should handle the errors instead of hiding
$url = "http://www.hello-world.com/"; //our target url
$dom = new DOMDocument();
$dom->loadHTML(file_get_contents($url)); //send the html to our dom
foreach($dom->getElementsByTagName('a') as $a) //for each link (<a></a>)
{
	$a->attributes->getNamedItem('href')->nodeValue = 'http://www.lolcaption.com/wp-content/uploads/2009/11/funny-demotivational-posters-girl-scouts-cookies.jpg'; //set the value of the href attribute to whatever we want
}
echo $dom->saveHTML(); //echo to screen
?>

Was This Post Helpful? 1
  • +
  • -

#7 itdoell  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 239
  • Joined: 13-January 11

Re: Anyone know how to work with a html file to generate?

Posted 27 June 2011 - 06:42 PM

I'm confused by your question, I'm also confused why on earth you'd want to create it in that manner. Why don't you just store information in a database for manipulatable content? So say you have a video (like in your example) you could store the source of the video in a database. Then you could just set up a simple little admin page which will display all the posted videos, the administrator could then just hunt down the video they want to remove, hit a button which will then remove remove that specific video from the database. You can also do this if you want to replace old content with new content, etc etc.
Was This Post Helpful? 0
  • +
  • -

#8 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: Anyone know how to work with a html file to generate?

Posted 26 July 2011 - 11:59 PM

View Postitdoell, on 27 June 2011 - 07:42 PM, said:

I'm confused by your question, I'm also confused why on earth you'd want to create it in that manner. Why don't you just store information in a database for manipulatable content? So say you have a video (like in your example) you could store the source of the video in a database. Then you could just set up a simple little admin page which will display all the posted videos, the administrator could then just hunt down the video they want to remove, hit a button which will then remove remove that specific video from the database. You can also do this if you want to replace old content with new content, etc etc.



ya, but this be for the client to remove video files from the file.

Like they are designing their own webpage layout. They can add thing and later on remove them.

so I need to have these in a file that is their own page. I then need to manipulate the file contents which are php,javascript and html and css.

how can you select the exact code and like toss that to a function or something... like a search function to find a match and replace it with a blank. I know php has a string replace function.

how would one do this?
Was This Post Helpful? 0
  • +
  • -

#9 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Anyone know how to work with a html file to generate?

Posted 27 July 2011 - 05:24 AM

Check the code in my previous post.
Was This Post Helpful? 1
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2992
  • Posts: 10,337
  • Joined: 08-August 08

Re: Anyone know how to work with a html file to generate?

Posted 27 July 2011 - 08:01 AM

Study Codeprada's example! If your client wants to design their own web pages then you definitely want them to only have to deal with HTML. Using DOM along with my other techniques for separating code should allow you to do anything you like to the HTML.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1