0

I am trying to find a word (no spaces) in a website and decided to do it using PHP. As a newbie in PHP I searched the internet and found the below code in order to search all the pages of a site

<?php
$ch = curl_init("https://www.php.net/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("search" => "RC6"));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>

The problem is that this is returning the whole website's source code so maybe is not the correct way to approach it. The above is an example of what I want to execute. So if I need to search for a word containing specific characters and its length to be exactly 8 characters, what is the best way to implement it?

J-K
  • 139
  • 11
  • Not sure if you are expecting a specific function to search a web page for some text, but loading the page and searching for the text in the result may work. – Nigel Ren May 22 '20 at 06:51
  • @NigelRen can I search for all the pages of that site or only a specific link? – J-K May 22 '20 at 06:54
  • Using most methods you will load 1 page and even then the page may use javascript to display the content and therefore you won't find it. But if you want all of the pages you would have to be able to follow the links yourself. – Nigel Ren May 22 '20 at 06:56
  • it depends on what the website can provide you, some may have an api for free to use, or you may need to do some web scraping (and this maybe against their terms) – Kevin May 22 '20 at 07:02
  • 1
    but for what its worth, php.net has its own search in their site, so just programmatically submit a get request in their `php.net/search.php?q=` and it will yield pertinent information – Kevin May 22 '20 at 07:03
  • @Kevin php.net/ is just an example. I did not want to add my site here. I just need to find that word with the exact text length and a given character – J-K May 22 '20 at 07:05
  • using curl is one way to get your desired webpage, you get the response as a string, of course you're dealing with an html document so you're better of using a parser like `DOMDocument`, you can't just use `array("search" => "RC6")` and magically yield you your desired result – Kevin May 22 '20 at 07:08
  • can you provide me the correct way or a code modification for my case? – J-K May 22 '20 at 07:10
  • your question is too broad to give you a specific answer, try to get your feet wet first. here's the idea: get the webpage using curl (which you already have), using the response that you got, you need to parse the whole document and search for your desired string, use [`DOMDocument`](https://stackoverflow.com/questions/2571232/parse-html-with-phps-html-domdocument). this should get you moving, although this isn't the only way, there are numerous ways of doing it. the website that contains your desired information may even have its own api to use, so that's even better way to get data – Kevin May 22 '20 at 07:13
  • "Website" is just a blob of characters, thus str_pos() could work as well (as a simple solution to start off) – Eriks Klotins May 22 '20 at 07:28
  • If it's a remote website it will be far quicker and far smoother to use a search engine: `site:php.net RC6` . I think the search engine could also return cURL requests to this effect as well.... – Martin May 22 '20 at 09:14

0 Answers0