I’m trying to strip the numeric and punctuations from a string leaving only alpha characters in SIMPLE HTML DOM, with no success I’ve tried multiple approaches and just can’t get it!
Example string: The Amazing Retard (2012) #1
Output string: The Amazing Retard
I understand it’s for an undefined method and I’ve looked at multiple pages for this, however I’m brain farting for how to include the method. Any help would be appreciated. The error that I get is
Fatal error: Call to undefined method simple_html_dom_node::preg_replace() in /home/**/public_html/wp-content/themes/*/***.php on line 123
The code is as follows:
<?php
function scraping_comic()
{
// create HTML DOM
$html = file_get_html('http://page-to-scrape.com');
// get block
foreach($html->find('li.browse_result') as $article)
{
// get title
$item['title'] = trim($article->find('h4', 0)->find('span',0)->outertext);
// get title url
$item['title_url'] = trim($article->find('h4', 0)->find('a.grid-hidden',0)->href);
// get image
$item['image_url'] = trim($article->find('img.main_thumb',0)->src);
// get details
$item['details'] = trim($article->find('p.browse_result_description_release', 0)->plaintext);
// get sale info
$item['on_sale'] = trim($article->find('.browse_comics_release_dates', 0)->plaintext);
// strip numbers and punctuations
$item['title2'] = trim($article->find('h4',0)->find('span',0)->preg_replace("/[^A-Za-z]/","",$item['title2'], 0)->plaintext);
$ret[] = $item;
}
// clean up memory
$html->clear();
unset($html);
return $ret;
}
// -----------------------------------------------------------------------------
$ret = scraping_comic();
if ( ! empty($ret))
{
$scrape = 'http://the-domain.com';
foreach($ret as $v)
{
echo '<p>'.$v['title2'].'</p>';
echo '<p><a href="'.$scrape.$v['title_url'].'">'.$v['title'].'</a></p>';
echo '<p><img src="'.$v['image_url'].'"></p>';
echo '<p>'.$v['details'].'</p>';
echo '<p> '.$v['on_sale'].'</p>';
}
}
else { echo 'Could not scrape site!'; }
?>
preg_replaceis a php function, not a member of thesimple_html_dom_nodeclass. call it like this:http://php.net/manual/en/function.preg-replace.php
it looks like your
$patternandreplacementare OK; you’ll just pass in as the$subjectthe input you’re trying to change.for example, this might be what you’re trying to achieve: