Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulty generating a slug with / and , #73

Open
LeandroCD opened this issue Jan 5, 2024 · 1 comment
Open

Difficulty generating a slug with / and , #73

LeandroCD opened this issue Jan 5, 2024 · 1 comment

Comments

@LeandroCD
Copy link

When generating a slug that contains / and , , it replaces these characters with nothing, when it should correctly replace them with a separator.

  • Test string: Bomba Submersa 1/4HP 0,25 110V Lepono
  • Incorrect: bomba-submersa-14hp-025-110v-lepono
  • Correct: bomba-submersa-1-4hp-0-25-110v-lepono

I modified the following code snippet:

$string = (string) \preg_replace(
            [
                // 1) remove un-needed chars
                '/[^' . $separatorEscaped . $removePatternAddOn . '\-a-zA-Z0-9\s]/u',
                // 2) convert spaces to $separator
                '/[\s]+/u',
                // 3) remove some extras words
                $removeWordsSearch,
                // 4) remove double $separator's
                '/[' . ($separatorEscaped ?: ' ') . ']+/u',
                // 5) remove $separator at the end
                '/[' . ($separatorEscaped ?: ' ') . ']+$/u',
            ],
            [
                '',
                $separator,
                '',
                $separator,
                '',
            ],
            $string
        );

To:

$string = (string) \preg_replace(
            [
                // 1) remove un-needed chars
                '/[^' . $separatorEscaped . $removePatternAddOn . '\-a-zA-Z0-9\s]/u',
                // 2) convert spaces to $separator
                '/[\s]+/u',
                // 3) remove some extras words
                $removeWordsSearch,
                // 4) remove double $separator's
                '/[' . ($separatorEscaped ?: ' ') . ']+/u',
                // 5) remove $separator at the end
                '/[' . ($separatorEscaped ?: ' ') . ']+$/u',
            ],
            [
                $separator,
                $separator,
                '',
                $separator,
                '',
            ],
            $string
        );

And it worked correctly.

@lux
Copy link
Collaborator

lux commented Jan 11, 2024

Ah yes, it simply strips those out by default. An easier way to add them would be to do this:

<?php

require_once 'vendor/autoload.php';

URLify::add_chars ([
	'/' => '-',
	',' => '-'
]);

echo URLify::slug ('Bomba Submersa 1/4HP 0,25 110V Lepono') . PHP_EOL;

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants