Skip to content
site logo mobile

Forum in maintenance, we will back soon 🙂

Phyton & WordPress ...
 
Notifications
Clear all

Phyton & WordPress ( I have started to learn coding)

5 Posts
3 Users
0 Likes
468 Views
(@4dg8)
Posts: 2
New Member
Topic starter
 

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?

 

 

 
Posted : 08/09/2023 10:22 am
SSAdvisor
(@ssadvisor)
Posts: 955
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@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

 
Posted : 08/09/2023 8:30 pm
(@4dg8)
Posts: 2
New Member
Topic starter
 

@ssadvisor  I have tried but with bad

results

 
Posted : 08/10/2023 4:47 am
Hasan Aboul Hasan
(@admin)
Posts: 984
Member Admin
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@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://learnwithhasan.com/create-ai-tools-with-wordpress/

 
Posted : 08/10/2023 11:34 am
SSAdvisor
(@ssadvisor)
Posts: 955
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@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

 
Posted : 08/11/2023 2:03 am
Share: