Laravel str_slug not working for unicode bangla - php

I am working in a laravel project. I have slugged url. Its working fine for English language. But while I use Bangla it returns empty. Please help me to solve the issue.
echo str_slug("hi there");
// Result: hi-there
echo str_slug("বাংলাদেশ ব্যাংকের রিজার্ভের অর্থ চুরির ঘটনায় ফিলিপাইনের");
// Result: '' (Empty)

str_slug or facade version Str::slug doesn't work with non-ascii string. You can instead use this approach
function make_slug($string) {
return preg_replace('/\s+/u', '-', trim($string));
}
$slug = make_slug(" বাংলাদেশ ব্যাংকের রিজার্ভের অর্থ চুরির ঘটনায় ফিলিপাইনের ");
echo $slug;
// Output: বাংলাদেশ-ব্যাংকের-রিজার্ভের-অর্থ-চুরির-ঘটনায়-ফিলিপাইনের

certainly, this code will be suitable for any local language. you can use for unicode or any other operation. this preg_match will remove some of special character and convert seo friendly slug from your post title.
enter code here
function CleanURL($string, $delimiter = '-') {
$string = preg_replace("/[~`{}.'\"\!\#\#\$\%\^\&\*\(\)\_\=\+\/\?\>\<\,\[\]\:\;\|\\\]/", "", $string);
$string = preg_replace("/[\/_|+ -]+/", $delimiter, $string);
return $string;
}
$slug=CleanURL($request->title);
$post->slug=$slug;

Try this; it will work properly.
$('input[name=title]').keyup(function () {
var slugElm = $('input[name=slug]');
slugElm.val(
this.value.toLowerCase()
.replace(this.value, this.value).replace(/^-+|-+$/g, '')
.replace(/\s/g, '-')
)
})

Related

Remove Arabic Diacritic

I want php to convert this...
Text : الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ
converted to : الحمد لله رب العالمين
I am not sure where to start and how to do it. Absolutely no idea. I have done some research, found this link http://www.suhailkaleem.com/2009/08/26/remove-diacritics-from-arabic-text-quran/ but it is not using php. I would like to use php and covert the above text to converted text. I want to remove any diacritic from user input arabic text
The vowel diacritics in Arabic are combining characters, meaning that a simple search for these should suffice. There's no need to have a replace rule for every possible consonant with every possible vowel, which is a little tedious.
Here's a working example that outputs what you need:
header('Content-Type: text/html; charset=utf-8', true);
$string = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';
$remove = array('ِ', 'ُ', 'ٓ', 'ٰ', 'ْ', 'ٌ', 'ٍ', 'ً', 'ّ', 'َ');
$string = str_replace($remove, '', $string);
echo $string; // outputs الحمد لله رب العالمين
What's important here is the $remove array. It looks weird because there's a combining character between the ' quotes, so it modifies one of those single quotes. This might need saving in the same character encoding as your text is.
try this:
$string = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';
$string = preg_replace("~[\x{064B}-\x{065B}]~u", "", $string);
echo $string; // outputs الحمد لله رب العالمين
Try this code, it's works fine:
<?php
$str = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';
$unicode = [
"~[\x{0600}-\x{061F}]~u",
"~[\x{063B}-\x{063F}]~u",
"~[\x{064B}-\x{065E}]~u",
"~[\x{066A}-\x{06FF}]~u",
];
$str = preg_replace($unicode, "", $str);
echo $str;
?>
See: Arabic unicode
Thank's for: Hosein Shahrestani
I'm not Arabic speaking, but i think you can make some alphabet remap:
function remap($string) {
$remap = [
'ą' => 'a',
'č' => 'c',
/* ... Arabic alphabet remap */
];
return str_replace(array_keys($remap), $remap, $string);
}
echo remap('ąčasdadfg'); // => acasdadfg

PHP slug function for all languages

I'm wondering if there is a simple function/code that can take care of creating a slug from a given string.
I'm working on a multilingual website (English, Spanish, and Arabic) and I'm not sure how to handle that for Spanish and Arabic specifically.
I'm currently using the below code from CSS-Tricks but it doesn't work for UTF-8 text.
<?php
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>
If you would like to use the same text without translation
function slug($str, $limit = null) {
if ($limit) {
$str = mb_substr($str, 0, $limit, "utf-8");
}
$text = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
return $text;
}
By: Walid Karray
I created the library ausi/slug-generator for this purpose.
It uses the PHP Intl extension, to translate between different scripts, which itself is based on the data from Unicode CLDR. This way it works with a wide set of languages.
You can use it like so:
<?php
$generator = new SlugGenerator;
$generator->generate('English language!');
// Result: english-language
$generator->generate('Idioma español!');
// Result: idioma-espanol
$generator->generate('لغة عربية');
// Result: lght-rbyt

How to make a Slug from the Title

I' am creating a slug on the fly. When I review the database my slug row looks like this
laal-salaam---2002
What actually I don't want is duplicate hyphen between the words.
$crawl_slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $crawl_name);
$crawl_slug = strtolower($crawl_slug);
Thats the PHP code that handles in making the slug from the name on the fly.
The end result should be
laal-salaam-2002
Is there any other way I can achieve this issue. Thanks!
This is a Simple Function I use for years.
<?php
function to_slug($string)
{
$string = trim($string);
$string1 = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
return preg_replace("/\-+/i", "-", $string1);
}
$slug = to_slug("laal-salaam---2002");
echo $slug
?>

PHP getting str_replace to work with a GET request?

So, Jessica was kind enough to help me last time.
<?php
$string = 'hi+';
if(strpos($string, '+') !== false){
$string = str_replace('+', 'test', $string);
}
echo $string;
?>
Would basically work, however changing $string to a GET request like $string = $_GET['s']; and testing that would not take the + as user input (would just be blank). How would I fix this? Would it work with POST?
When in a GET parameter, the + gets urlencoded as %2B. When grabbing it from the query string, you need to urldecode it.:
$string = urldecode($_GET['s']);
Example
And if your url's aren't properly encoded, you can force encoding and you get desired result.
Something like that should work:
<?php
$string = urlencode($_GET['s']);
if(strpos($string, '+') !== false){
$string = str_replace('+', 'test', $string);
}
echo $string;
But i say: the correct way is decoding the encoded URL, not doing reverse (encoding to get the raw URL from already decoded one.)

Replace all occurrences of \\ not starting with

This should be simple. I want to change all of these substrings:
\\somedrive\some\path
into
file://\\somedrive\some\path
but if substrings already have a file:// then I don't want to append it again.
This doesn't seem to do anything:
var_export( str_replace( '\\\\', 'file://\\\\', '\\somedrive\some\path file://\\somedrive\some\path' ) );
What am I doing wrong? Also, the above doesn't take into test for file:// already being there; what's the best way of dealing with this?
UPDATE test input:
$test = '
file://\\someserver\some\path
\\someotherserver\path
';
test output:
file://\\someserver\some\path
file://\\someotherserver\path
Thanks.
You should consider escape sequence in string also.
if((strpos($YOUR_STR, '\\\\') !== false) && (strpos($YOUR_STR, 'file://\\\\') === false))
var_export( str_replace( '\\\\', 'file://\\\\', $YOUR_STR ) );
Use a regular expression to check if the given substring starts with file://. If it does, don't do anything. If it doesn't, append file:// at the beginning of the string:
if (!preg_match("~^file://~i", $str)) {
$str = 'file://' . $str;
}
As a function:
function convertPath($path) {
if (!preg_match("~^file://~i", $path)) {
return 'file://'.$path;
}
return $path;
}
Test cases:
echo convertPath('\\somedrive\some\path');
echo convertPath('file://\\somedrive\some\path');
Output:
file://\somedrive\some\path
file://\somedrive\some\path
EDIT
For multiple occurrences : preg_replace('#((?!file://))\\\\#', '$1file://\\\\', $path)
This will work to give you the output you are expecting. As php.net says double slash will be converted into single slash.
if (!preg_match('/^file:\/\//', $str)) {
$str = "file://\\".stripslashes(addslashes($str));
}
Please try this:
$string = "\\somedrive\some\path";
$string = "\\".$string;
echo str_replace( '\\\\', 'file://\\\\',$string);

Categories