Skip to content

Bug: Url Helper have a bug #3180

Description

@mmrtonmoybd

In url helper have a method url_title(). It can makes a slug from string. It also support Unicode. But it not totally support Unicode.
I tried to do a string to slug but codeigniter gives output:
$cname = "মশিউর রহমান";
echo url_title($cname, '-', TRUE);
Output: মশউর-রহমন

Expect: মশিউর-রহমান

I do few changes in system/Helpers/url_helper.php

In before
///////////////////////////////

function url_title(string $str, string $separator = '-', bool $lowercase = false): string
	{
		$q_separator = preg_quote($separator, '#');

		$trans = [
			'&.+?;'                   => '',
			'[^\w\d _-]'              => '',
			'\s+'                     => $separator,
			'(' . $q_separator . ')+' => $separator,
		];

		$str = strip_tags($str);
		foreach ($trans as $key => $val)
		{
			$str = preg_replace('#' . $key . '#iu', $val, $str);
		}

		if ($lowercase === true)
		{
			$str = mb_strtolower($str);
		}

		return trim(trim($str, $separator));
	}
}

/////////////////////////////
I do few changes
In after

function url_title(string $str, string $separator = '-', bool $lowercase = false, string $patran = ''): string
	{
		$q_separator = preg_quote($separator, '#');

		if ($patran != '') {
		  $trans = [
			'&.+?;'                   => '',
			$patran              => '',
			'\s+'                     => $separator,
			'(' . $q_separator . ')+' => $separator,
		];
		} else {
		   $trans = [
			'&.+?;'                   => '',
			'[^\w\d _-]'              => '',
			'\s+'                     => $separator,
			'(' . $q_separator . ')+' => $separator,
		];
		}

		$str = strip_tags($str);
		foreach ($trans as $key => $val)
		{
			$str = preg_replace('#' . $key . '#iu', $val, $str);
		}

		if ($lowercase === true)
		{
			$str = mb_strtolower($str);
		}

		return trim(trim($str, $separator));
	}
} 

////////////////

$cname = "মশিউর রহমান";
url_title($cname, '-', TRUE, '[^a-z0-9\x{0980}-\x{09ff}]');
Output: মশিউর-রহমান
It works and output same as my expected.
I add a pattern option that anyone can replace there string as they want.
I hope the changes will be add next version. If I do any worng. Please comment on this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions