Pros,

Imagine you want to only provide your download link to those who tweet about you. Now, how would you code it ?
Let's make it simple for you to understand.
Let's say you want to write a php script that checks if a certain html element (eg. name=username) or plain text exists or not.
If it exists then to give you an alert "TRUE"; Else alert: FALSE.

In this example, you want the script to check this page:

And you want it to check if the following text (exact match) exists or not:
"I'd like to put forward an Idea"

How would you code it ? Can we see 2-3 examples ?
I'd be grateful if you can show another example for a fuzzy match. Eg:

  • like to put forward *

    This is an interesting topic. Base the boolean around what exists or not on a page.
    Curious to see what the code would look like and php is capable of doing it in how many lines of code.

    Thanks

Dani AI

Generated

Short practical guide tied to the thread: for a site that serves plain HTML, a server-side fetch plus either a simple text search or a DOM query is the cleanest route; for JavaScript-rendered pages a headless browser is required; for Twitter-like use-cases the stable option is the platform API (as noted). The answers from and point in the right directions: use the API when available, learn the DOM basics for element-level checks, and prefer parsing tools over brittle regex when the page is structured.

Example — exact text search (fast, works for static HTML):

<?php
// fetch page (static HTML)
$ch = curl_init('https://twitter.com/TangoForce123');
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_USERAGENT => 'Mozilla/5.0',
]);
$html = curl_exec($ch);
curl_close($ch);

$needle = "I'd like to put forward an Idea";
$exists = (mb_strpos(html_entity_decode($html), $needle) !== false) ? 'TRUE' : 'FALSE';
echo $exists;

Element-level check with DOM+XPath (robust for structured HTML):

<?php
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($html);
libxml_clear_errors();

$xp = new DOMXPath($dom);
$nodes = $xp->query("//*[@name='username']"); // select elements with name="username"
echo ($nodes->length > 0) ? 'TRUE' : 'FALSE';

Simple fuzzy match (match partial phrase, safer if run on plain text):

<?php
$plain = html_entity_decode(strip_tags($html));
// look for "like to put forward" followed by at least one word
$found = preg_match('/\blike to put forward\b\s+\S+/iu', $plain);
echo $found ? 'TRUE' : 'FALSE';

Troubleshooting notes and cautions: handle encodings (use mb_* functions), respect HTTP status codes, set timeouts and User-Agent, follow redirects, and watch rate limits and TOS when scraping public sites. For content injected by JavaScript, use a headless browser (headless Chromium via Puppeteer/Playwright or similar) to render before checking. The approaches above are minimal examples; combining DOM extraction for structured checks and a lightweight regex/plain-text search for fuzzy matches gives the most reliable results.

Recommended Answers

All 4 Replies

In the specific case of Twitter, there is an API that allows to search through the public tweets of a specific account:

You can check the libraries used to connect this service here and see how it is done:

If you want to check the contents of a static page and here I mean the contents generated on server side and loaded in plain HTML, you can use a library to analyze the DOM, like:

DOM libraries require valid documents, if these are malformed then the extraction can fail.

If, instead, the contents are loaded through Javascript, you need a browser engine and some javascript, see as example:

A part these, there are many other available solutions, like regular expressions or scripting with command line tools as awk or sed:

Also, if the goal is to extract data, don't limit your choices to PHP, there are excellent solutions written in other languages, see for example Scrapy:

Curious to see what the code would look like and php is capable of doing it in how many lines of code.

Hehe, I think you can try to extract the tweet through a DOM library (DOM Crawler should be easy to use), check the HTML source of the link you provided, load the page and see what you can get from there.

Thank you!!

Cereal,

I don't understand all that DOM stuffs. ALthought, heard of it before.
Anyway, thankls for the Scrapy link.

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.