Forum in maintenance, we will back soon 🙂
I wanted to have a utility to count (words) in Google Blogger. I found this code (in phyton (attached)) and it is doing as expected.
Now I want to offer this service (free) on my WordPress site. I need your help to modify the code and write the HTML.
Can you help me?
@4dg8 I don't see anything attached but have you tried using ChatGPT to help you convert the Python code to a Wordpress PHP module?
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@4dg8 I see a Python code. you need to convert to PHP wp function and utilize with JS code as we did in this guide: https://staging.learnwithhasan.com/create-ai-tools-with-wordpress/
@4dg8 try with the following prompt:
You are an expert at converting Python code to similar functions in PHP. In the next prompt I will give you some code that I want converted. I want you to take a step by step approach and give me the completed code. Do you understand?
Here is the result when I tried this:
Let's break down the Python code and convert it into PHP step by step: 1. Importing necessary libraries: - `requests` in Python is used to make HTTP requests. In PHP, we'll use the `cURL` functions. - `BeautifulSoup` from `bs4` is used for parsing HTML. In PHP, we can use the `DOMDocument` class. - `re` is for regular expressions. PHP has its own functions for regular expressions, like `preg_replace()`. - `tabulate` for creating tables. PHP does not have a direct alternative, but we can manually format tables. 2. The `count_words` function: - Send a GET request to the URL. - Parse the HTML, remove script and style tags, and then count words. 3. Iterating over URLs and printing results in tabular format. Let's start converting: ```php <?php function count_words($url) { // Initialize cURL session $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode == 200) { $dom = new DOMDocument(); @$dom->loadHTML($html); // @ suppresses warnings // Remove script and style tags $script_tags = $dom->getElementsByTagName('script'); $style_tags = $dom->getElementsByTagName('style'); foreach ($script_tags as $tag) { $tag->parentNode->removeChild($tag); } foreach ($style_tags as $tag) { $tag->parentNode->removeChild($tag); } $text = $dom->textContent; $text = preg_replace('/\s+/', ' ', $text); // Remove multiple whitespaces and newlines $words = explode(' ', $text); return count($words); } else { echo "Failed to retrieve content from {$url}. Error code: {$httpCode}\n"; return 0; } } // List of URLs to process $urls = [ // Your URLs go here ]; $headers = ['URL', 'Word Count']; $rows = []; foreach ($urls as $url) { $word_count = count_words($url); $rows[] = [$url, $word_count]; } // Print the table echo "+".str_repeat("-", 50)."+" .str_repeat("-", 10)."+\n"; echo "|".str_pad("URL", 50). "|".str_pad("Word Count", 10)."|\n"; echo "+".str_repeat("-", 50)."+" .str_repeat("-", 10)."+\n"; foreach ($rows as $row) { echo "|".str_pad($row[0], 50). "|".str_pad($row[1], 10)."|\n"; } echo "+".str_repeat("-", 50)."+" .str_repeat("-", 10)."+\n"; ?> ``` This PHP code essentially replicates the behavior of the Python code provided, but there are some things to consider: - The error handling is basic, and it may require more comprehensive checks depending on the use case. - This PHP code outputs results directly. Depending on where you run this, you might want to refactor for output handling. - Iterating over the nodes to remove them in the DOMDocument can be tricky, this code assumes there's only a small number of scripts and styles. If there are many of these tags, you may need a different approach to efficiently remove them all. - Adjust the 50 and 10 numbers in the table output part of the script if you expect URLs or word counts to be longer/shorter. This will ensure the table formatting is consistent.
Take note of the "things to consider" as you may need to adjust the code.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack