Title2Slug is a PHP tool to generate SEO-friendly slugs for Persian product names. It reads names from a CSV file, calls an AI API (OpenAI or a third-party AI) in chunks, retries on invalid responses, and writes the results to a new CSV file. Additionally, it provides a script to check for duplicate slugs.
- Reads Persian product names from a CSV (
input.csv
). - Generates SEO-friendly slugs using AI in chunks of 10.
- Supports OpenAI API or a third-party AI API (
talkai.info
). - Automatic retry on invalid JSON or empty responses.
- Writes results to
output.csv
with an addedنامک
column. - Fully configurable via
PROMPT.txt
and script variables. - Checks for duplicate slugs using
check_duplicates.php
. - Easy to extend and integrate into other workflows.
- Clone the repository:
git clone https://github.com/BaseMax/title2slug.git
cd title2slug
-
Place your input CSV file as
input.csv
. The CSV must contain a columnنام
with product names. -
Edit
PROMPT.txt
to customize the AI prompt. Use$INPUTS
as a placeholder for the product names array. -
(Optional) Configure your OpenAI API key in
title2slug.php
:
$openaiApiKey = 'YOUR_OPENAI_API_KEY';
$openaiApi = true; // set to true to use OpenAI
Run the script from the command line:
php title2slug.php
The script will:
- Read
input.csv
. - Generate slugs in chunks.
- Retry if any chunk returns invalid JSON.
- Write the output to
output.csv
with an additionalنامک
column.
Example output CSV:
شناسه | نام | نامک |
---|---|---|
3433 | سیستم صوتی SSE2.v2 - فکتور | sse2v2-audio-system-factor |
3434 | پریز برق روکار UPO2 - فکتور | upo2-surface-mounted-power-outlet-factor |
3435 | شارژر وایرلس BWC1.v2 - فکتور | bwc1v2-wireless-charger-factor |
After generating slugs, you can check for duplicates with:
php check_duplicates.php
The script will list all duplicate نامک
entries along with their corresponding شناسه
values, helping you quickly identify conflicts.
- $inputCsv: Path to input CSV.
- $outputCsv: Path to output CSV.
- $promptFile: Path to AI prompt text file.
- $maxChunkSize: Number of items processed per AI request (default: 10).
- $openaiApi: Boolean to switch between OpenAI API and third-party AI.
- $openaiApiKey: Your OpenAI API key.
- The script handles JSON wrapped in triple backticks (```) from the third-party API.
- Slug count mismatches are automatically padded with empty strings.
- Ensure your CSV uses UTF-8 encoding to prevent Persian character issues.
check_duplicates.php
requiresoutput.csv
generated bytitle2slug.php
.
This project is licensed under the MIT License.
© 2025 Max Base