Asked  1 Year ago    Answers:  5   Viewed   10 times

hey guys, $form holds this...

<form method="post" action="">
    <input type="hidden" name="ip" value="">
    <label for="s2email">Your Email</label>
    <input type="text" name="email" id="s2email" value="email..." size="20" onfocus="if (this.value == 'email...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'email...';}">
    <input type="submit" name="subscribe" value="Subscribe">
    <input type="submit" name="unsubscribe" value="Unsubscribe">

Since I have no idea what's possible with the DOMDocument class I need to ask for help. I actually have two things I need to transform on the $form above.

1.) label-s2email and its input#sd-email should be wrapped inside a <div class="name"> Is that even possible to select those two (via id or for-attribute) and wrap those two into a div?

2.) Is it possible to remove the onfocus and onblur attribute with php?

thank you for your help



Since I cannot find a suitable duplicate that shows how to wrap nodes with DOM, here is the solution:

// Setup DOMDocument and XPath
$dom = new DOMDocument;
$xpath = new DOMXPath($dom);

// Create <DIV> with class attribute name
$div = $dom->createElement('div');
$div->setAttribute('class', 'name');

// Find <input id="s2email"> and remove event attributes
$input = $xpath->query('/html/body/form/input[@id="s2email"]')->item(0);

// Find <label for="s2email"> and insert new DIV before that
$label = $xpath->query('/html/body/form/label[@for="s2email"]')->item(0);
$label->parentNode->insertBefore($div, $label);

// Move <label> and <input> into the new <div>

// Echo the <form> outer HTML
echo $dom->saveHTML($dom->getElementsByTagName('form')->item(0));

The above code will produce (live demo):

<form method="post" action="">
    <input type="hidden" name="ip" value=""><div class="name">
<label for="s2email">Your Email</label><input type="text" name="email" id="s2email" value="email..." size="20">
    <input type="submit" name="subscribe" value="Subscribe"><input type="submit" name="unsubscribe" value="Unsubscribe">

Note that in order to pass a node to saveHTML, you need PHP 5.3.6. See

  • How to return outer html of DOMDocument?

for possible workarounds before that.

Thursday, April 1, 2021

This has really been one of the most desired features in Eclipse. It's not just missing in PHP files-- it's missing in the IDE. Fortunately, from Google Summer of Code, we get this plug-in Eclipse Word-Wrap

To install it, add the following update site in Eclipse:

AhtiK Eclipse WordWrap 0.0.5 Update Site

Thursday, April 1, 2021

You just include the extra <i></i> tag in the button $title, also include 'escape' => false to ensure the mark up is not escaped, even though is not escaped by default as on v3.1.1, this may change in future, who knows..


echo $this->Form->button("<i class='icon-search'></i> Search", array('type' => 'submit','id' => 'search_button', 'class' => 'searchbutton', 'escape' => false));
Saturday, May 29, 2021

As I commented it's better not to use getElementById. Better analyze my test sample; it works

$html = "<table ID='myid'><tr><td>1</td><td>2</td></tr><tr><td>4</td><td>5</td></tr><tr><td>7</td><td>8</td></tr></table>";

$xml = new DOMDocument();
$xml->validateOnParse = true;

$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[@id='myid']")->item(0);

// for printing the whole html table just type: print $xml->saveXML($table); 

$rows = $table->getElementsByTagName("tr");

foreach ($rows as $row) {
  $cells = $row -> getElementsByTagName('td');
  foreach ($cells as $cell) {
    print $cell->nodeValue; // print cells' content as 124578
Saturday, May 29, 2021

Quick and dirty, in c++

#include <sstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <memory.h>

using namespace std;

int cac[1000][1000];
string res[1000][1000];
vector<string> words;
int M;

int go(int a, int b){
    if(cac[a][b]>= 0) return cac[a][b];
    if(a == b) return 0;

    int csum = -1;
    for(int i=a; i<b; ++i){
    csum += words[i].size() + 1;
    if(csum <= M || a == b-1){
    string sep = "";
        for(int i=a; i<b; ++i){
            sep = " ";
    return cac[a][b] = (M-csum)*(M-csum);

    int ret = 1000000000;
    int best_sp = -1;
    for(int sp=a+1; sp<b; ++sp){
    int cur = go(a, sp) + go(sp,b);
    if(cur <= ret){
        ret = cur;
        best_sp = sp;
    res[a][b] = res[a][best_sp] + "n" + res[best_sp][b];
    return cac[a][b] = ret;

int main(int argc, char ** argv){
    memset(cac, -1, sizeof(cac));
    M = atoi(argv[1]);
    string word;
    while(cin >> word) words.push_back(word);
    go(0, words.size());
    cout << res[0][words.size()] << endl;


$ echo "The quick brown fox jumps over a lazy dog" |./a.out 10
The quick
brown fox
jumps over
a lazy dog

EDIT: just looked at the wikipedia page for minimum raggedness word wrap. Changed algorithm to the given one (with squared penalties)

Tuesday, August 10, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :