Good day.
I want to create a small app that would take out the content from a rss feed and posteriorly from the url .
I know that would be easily done in perl with regular expressions, but in php it seams a bit more difficult.
So i want to translate the next querry from perl to php :

$mystring="startABCend";
if($mystring =~ m/start(.*)end/) {
	print $1;
}

Basically what it does is selecting any string from the middle of the string, so it would show "ABC".
Thanks in advance.

Dani AI

Generated

As answered, a regex works for the tiny example in the original post, but for RSS feeds and the pages they point to a parser-based workflow is far more reliable and maintainable. Typical steps: parse the RSS XML (many feeds contain full content already), then — only when needed — fetch the linked page and extract the article HTML with a proper HTML parser rather than trying to parse HTML with ad-hoc regexes.

Example workflow using built-in PHP tools:

$feed = simplexml_load_file('https://example.com/feed.xml');
foreach ($feed->channel->item as $item) {
  $link = (string)$item->link;
  $contentNode = $item->children('http://purl.org/rss/1.0/modules/content/')->encoded;
  $content = $contentNode ? (string)$contentNode : (string)$item->description;

  // if you still need the remote page, fetch it and parse with DOM/XPath
}

Fetch + extract example pattern (safe basic approach):

function fetch_html($url){
  $ch = curl_init($url);
  curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_FOLLOWLOCATION=>true, CURLOPT_USERAGENT=>'Mozilla/5.0', CURLOPT_TIMEOUT=>10]);
  $html = curl_exec($ch); curl_close($ch);
  return $html;
}

$html = fetch_html($link);
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//article | //div[contains(@class,'content')]");

Notes and troubleshooting tips:

  • Many feeds provide full HTML in content:encoded — check that first to avoid extra requests.
  • Use mb_convert_encoding and libxml_use_internal_errors to handle encodings and broken HTML.
  • For large feeds use XMLReader or a library like SimplePie (handles caching, namespaces, quirks).
  • If pages render content with JavaScript, server-side fetches won't see it; use a headless browser or an API.
  • Respect robots.txt/rate limits and cache results.

Good references: SimpleXML Manual, DOMDocument Manual, cURL Manual, and the SimplePie project for a higher-level feed parser (https://simplepie.org/).

Recommended Answers

All 2 Replies

Good day.
I want to create a small app that would take out the content from a rss feed and posteriorly from the url .
I know that would be easily done in perl with regular expressions, but in php it seams a bit more difficult.
So i want to translate the next querry from perl to php :

$mystring="startABCend";
if($mystring =~ m/start(.*)end/) {
	print $1;
}

Basically what it does is selecting any string from the middle of the string, so it would show "ABC".
Thanks in advance.

http://php.net/preg_match

$string = 'startABCend';
if (preg_match('/start(.*)end/', $string, $matches)) {
  echo $matches[1];
}

Thanks for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.