php - Removing unwanted elements from table simple_html_dom -
i fetching page page style tags, table , other non vital content. i'm storing in transient, , fetching ajax
$result_match = file_get_contents( 'www.example.com' ); set_transient( 'match_results_details', $result_match, 60 * 60 * 12 ); $match_results = get_transient( 'match_results_details' ); if ( $match_results != '') { $html = new simple_html_dom(); $html->load($match_results); $out = ''; $out .= '<div class="match_info_container">'; if (!empty($html) && is_object($html)) { foreach ($html->find('table') $table => $table_value) { $out .= preg_replace('/href="?([^">]+)"/', '', $table_value); } } $out .= '</div>'; wp_die ( $out ); } else { $no_match_info = esc_html__('no info available', 'kompisligan'); wp_die($no_match_info); }
now table had anchors , needed remove that, used preg_replace
find anchor , empty out. know can manipulate contents find()
method, had no success that.
now rid of entire <tfoot>
tag, , contains.
but every time try 'find' something, ajax returns error, meaning in code wrong.
how manipulate contents of found element simple_html_dom
? tried outputting contents of $html
can see i'll out ajax call lasts forever , cannot out.
you try this, using builtin domdocument instead of simple_html_dom. however, if ajax call timing out, might different problem (not being able load example.com or so).
if ( $match_results != '') { $html = new domdocument(); // suppress errors @$html->loadhtml($match_results); $out = '<div class="match_info_container">'; // remove "href" tags <a> foreach($html->getelementsbytagname('a') $href) $href->setattribute('href', ''); // remove tfoot foreach($html->getelementsbytagname('tfoot') $tfoot) $tfoot->parentnode->removechild($tfoot); // put contents of every <table> in div. foreach($html->getelementsbytagname('table') $table) $out .= $table->nodevalue; $out .= '</div>'; wp_die ( $out ); } else {
Comments
Post a Comment